DEV Community

loading...
Cover image for JavaScript Interview Question #30: Reject inside resolve

JavaScript Interview Question #30: Reject inside resolve

coderslang profile image Coderslang: Become a Software Engineer Originally published at learn.coderslang.com on ・1 min read

js-test-30

What will be logged to the console? Will the finally block be executed?

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

To analyze this code snippet, I'll start with the things that are clear:

  • the .then(console.log) function will not be executed and there’s a rejection inside of the Promise.resolve()
  • the catch block won’t be able to catch the rejection as it happens asynchronously

So, we’re left with the finally block. There’s a single call to the console.log and it’s the first message the will be printed on the screen.

Then, the unhandled rejection will happen as we haven’t provided the error handler to the promise chain in line 2.


ANSWER: The string finally will be logged to the console followed by the UnhandledPromiseRejectionWarning: -1

Learn Full-Stack JavaScript

Discussion (0)

pic
Editor guide