Member-only story
Tricks about Handle Promise Exception You May Don’t Know
The Promise always is an important but complex point in JavaScript. But when I review other coders’ ways to handle Promise exceptions, there often has some mistakes. Here, let’s discuss this question.
Handle Promise Exception with Try Catch
No more words, let’s look at a code snippet:
What is the result after running the above code? Can the try-catch statement really catch the exception which was thrown by the Promise object?
Here is the running result:
We can find that the try-catch statement can not catch the exception thrown in the foo
function.
Why?
Before we discuss the question, we should understand the execution order of the above code.