codecks.

Async & promises · Output question

JavaScript promise chains and returned values

A promise reaction transforms a value and returns it. Predict which value the next reaction receives.

Promise.resolve(3)
  .then(value => value + 2)
  .then(value => { console.log(value); })
  .then(value => console.log(value));

What gets logged?

Choose an output before opening the walkthrough, or check your answer in practice mode.

  1. 5
    undefined
  2. 5
    5
  3. 3
    undefined
  4. undefined
    undefined
Reveal the answer and execution trace
5
undefined

The first callback returns 5. The next callback logs 5 and returns nothing, so its resulting promise fulfills with undefined.

  1. Line 2. Returning value + 2 fulfills the next promise with 5.
  2. Line 3. This block logs 5, then implicitly returns undefined.
  3. Line 4. The final callback receives undefined.

Reference: Promise chain values on MDN.