Member-only story

Tricks about Handle Promise Exception You May Don’t Know

How to do in nested Promise?

Shuai Li
4 min readSep 12, 2021
Photo by Markus Spiske on Unsplash

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.

--

--

Shuai Li
Shuai Li

Written by Shuai Li

An enthusiastic game player of Earth Online.

No responses yet