DEV Community

Cover image for JS Test #9: Promise.reject + try/catch
Coderslang: Become a Software Engineer
Coderslang: Become a Software Engineer

Posted on • Originally published at learn.coderslang.com

8 1

JS Test #9: Promise.reject + try/catch

Will we catch the error in the catch block?

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

Regular try/catch blocks only catch errors that appear in the synchronous code.

As the Promise in the second line doesn’t have its own asynchronous .catch block, the rejection will be left unhandled.

An UnhandledPromiseRejectionWarning will be raised and the code inside of the regular catch block will not be executed.


ANSWER: The error will not be caught and the message the error was caught! will NOT be logged to the console.

Top comments (1)

Collapse
 
wangjing013 profile image
wangjing013

nice

This post blew up on DEV in 2020:

js visualized

🚀⚙️ JavaScript Visualized: the JavaScript Engine

As JavaScript devs, we usually don't have to deal with compilers ourselves. However, it's definitely good to know the basics of the JavaScript engine and see how it handles our human-friendly JS code, and turns it into something machines understand! 🥳

Happy coding!