2
votes

I have some questions regarding promise and axios.

In the below axios example, I am wondering how is it defined to run the catch code.

Is it based on response http status code? like 400?

axios.get('/user?ID=12345')
  .then(function (response) {
    console.log(response);
  })
  .catch(function (response) {
    console.log(response);
  });

Thanks.

2

2 Answers

2
votes

My understanding is that if any sort of networking issue occurs or if an error code is responded from the server then the promise is rejected.

An error code from the server is either a client error (4XX) or server error (5XX). Status code definitions.

In the tests in the source code you can see what the maintainers are testing for when rejecting the promise.

1
votes

You can find the answer from the source from axios.

https://github.com/mzabriskie/axios/blob/62db26b58854f53beed0d9513b5cf18615c64a2d/lib/defaults.js#L73

axios will check validateStatus, if status >= 200 && status < 300 , it will resolve. else if status < 200 && status >= 300, it will reject.