dumb async questions
Hi, I never really was able to deeply await promises and async functions. I have a couple of questions on error handling.
(axios)
How can I save a variable from an async get or post using .then on the top scope.
Is it possible to error handle an await statement instead of it always crashing my run
3 Replies
i think u need to use try and catch. if it throw res status other than 2xx example: 401 or 500, it will be threated as error and the response or json they send back will go to err.response.data iirc(try consolelog)
you can do something like that
just create a variable in the scope above and in the then scope assign your response to the variable. And also there's
try catch
for errors with async/await
and there's .catch()
method in the regular promisesThank you so much for the code snippets, I was trying to do the second example earlier but I ran into weird undefined issues.
I just used try and catch to fix most of my issues