from what I've read, using try/catch
is the "right" way to handle errors when using async/await
. However, I ran into an issue in trying to use the response of a request if I put it in a try/catch
block:
try {
async someMethod () {
const result = await someRequest()
}
} catch (error) {
console.log(error)
}
console.log(result) // cannot access `result` because it is not defined...
Therefore, is there a better way to handle errors AND be able to access request responses from async/await
calls? The only other way I can think of is to put the ENTIRE code block inside of the try/catch
block.. but I feel like there is a more elegant way..
Thanks in advance!