Skip to content

Latest commit

 

History

History
43 lines (31 loc) · 939 Bytes

3.Promise-then-callbacks.md

File metadata and controls

43 lines (31 loc) · 939 Bytes

3. Promise then callbacks

Problem

https://bigfrontend.dev/quiz/3-promise-then-callbacks

Problem Description

What does the code snippet below output by console.log?

Promise.resolve(1)
  .then(() => 2)
  .then(3)
  .then((value) => value * 3)
  .then(Promise.resolve(4))
  .then(console.log);

Answer

6

Explanation

If the first argument that .then() receives is not a function, it is internally replaced with a function that returns the receiving argument. Therefore, when the code snippet above runs, 3 and Promise.resolve(4) are going to be replaced with the function (result) => result:

Promise.resolve(1)
  .then(() => 2)
  // 3 is internally replaced with a function.
  .then((result) => result) // 2
  .then((value) => value * 3) // 6
  // Promise.resolve(4) is internally replaced with a function.
  .then((result) => result) // 6
  .then(console.log); // logs 6