0

I send a request like this

try {
 const res = await axios.post('/order', formData)

 console.log(res) --> undefined
 console.log(test1) --> it is on console
}
catch (error) {
 console.log(test2)
}

but for some reason even I get 500 from request, test1 and undefined are on console. Why is this happening even it is an error. Shouldnt it go to catch?

--- SOLUTION ---

I found what is wrong after 4 days. After I removed axios.interceptor from code everything works as expected

Furkan
  • 85
  • 1
  • 7
  • Usually with Promises you do `.post().catch(function () {console.log(test2)})`, not with try/catch, as your try/catch is executed sync – Justinas Feb 18 '21 at 22:07
  • Even I use axios .post('/order', formData) .then((res) => console.log('test1')) .catch((err) => console.log('test2')) I get test1 on console ( Still getting 500 from request ). I get error from server, how can it go to then block. Really weird – Furkan Feb 18 '21 at 22:13
  • @Justinas With `await` you should be able to use `try/catch` as like as you use `then/catch`. If the promise reject you will end up in the `catch` branch. I would check if the real status code of the response is 500, and not some other header maybe – DDomen Feb 18 '21 at 22:17
  • @DDomen I opened network and Its status 500 .Also there is an error which says my formData is wrong. How can it behave like success? – Furkan Feb 18 '21 at 22:19
  • By the way even it goes to then block, it doesnt return anything my res is undefined – Furkan Feb 18 '21 at 22:22

0 Answers0