Reputation: 911
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.
Upvotes: 2
Views: 829
Reputation: 11
You can find the answer from the source from axios.
axios will check validateStatus, if status >= 200 && status < 300 , it will resolve. else if status < 200 && status >= 300, it will reject.
Upvotes: 1
Reputation: 7550
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.
Upvotes: 2