Simple async question.
Promise.resolve(1)
.then((v) => { v + 1; })
.then((v) => console.log(v));
Why does this print undefined, and what one-line change fixes it.
Quelly
Simple async question.
Promise.resolve(1)
.then((v) => { v + 1; })
.then((v) => console.log(v));
Why does this print undefined, and what one-line change fixes it.
Quelly
Because the first .then callback uses braces and doesn’t return, it returns undefined.
Arthur
Yep - with {} in an arrow function, you need an explicit return, otherwise the next .then gets undefined.
WaffleFries
It prints undefined because that callback completes without yielding a value, and the terse fix is then(v => v + 1) or return v + 1.
Hari
:: Copyright KIRUPA 2024 //--