Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
100
rated 0 times [  107] [ 7]  / answers: 1 / hits: 9469  / 4 Years ago, thu, may 7, 2020, 12:00:00

Imagine in my app I have:


   let res = await axios({
url: 'https://test.com/posts/1',
method: 'get',
timeout: 8000,
headers: {
'Content-Type': 'application/json',
}
})

console.log("Success");


So you can see that I don't manually check the response status code of above call, rather assume that if "Success" was printed on console, the above call was success, otherwise there would be an exception or promise rejection and JS wouldn't continue till console.log.


Is this safe assumption?


More From » axios

 Answers
0

By default axios throws an error for non 2xx responses, so you can assume success at the point of your console.log. You'll of course want to catch errors and handle appropriately so you don't have an unhandled rejection issue.



If you don't want this behavior, you can use the validateStatus config option


[#3887] Tuesday, May 5, 2020, 4 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
dylondaytond

Total Points: 92
Total Questions: 88
Total Answers: 96

Location: China
Member since Fri, Jan 15, 2021
3 Years ago
dylondaytond questions
Tue, Jun 22, 21, 00:00, 3 Years ago
Fri, Aug 16, 19, 00:00, 5 Years ago
;