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.