DEV Community

Cover image for JavaScript Interview Question #19: Catching the rejected Promise
Coderslang: Become a Software Engineer
Coderslang: Become a Software Engineer

Posted on • Originally published at learn.coderslang.com on

JavaScript Interview Question #19: Catching the rejected Promise

js-test-19

Can you catch the Promise rejection in JS? Another unhandled rejection?

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

In JS, it’s impossible to catch the unhandled promise rejection using the regular try/catch blocks.

So, if the rejection does take place, then we’ll likely see a message like UnhandledPromiseRejectionWarning … or something along these lines.

Here, though, we don’t get to reject the promise properly.

JavaScript tries to evaluate the result of null.length which happens synchronously. An error Cannot read property 'length' of null will be thrown and caught in the catch block.


ANSWER: the error will be caught and the string the error was caught! Cannot read property 'length' of null will be logged to the screen.

Learn Full Stack JavaScript

Top comments (2)

Some comments may only be visible to logged-in visitors. Sign in to view all comments.