https://bigfrontend.dev/quiz/3-promise-then-callbacks
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);
6
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