I looked at many examples today. They seem to suggest that the following code should be executed in chain:
let f = () => {
return new Promise((res, rej) => {
console.log('entering function');
setTimeout(() => {
console.log('resolving');
res()
}, 2000)
});
};
Promise.resolve()
.then(f())
.then(f());
Expected output would be:
entering function
resolving
entering function
resolving
But it isn't. The output is
entering function
entering function
resolving
resolving
and I can't figure out why. Any help will be much appreciated.
try then(f) instead of then(f())
then expects a function.
you can also do then(()=>f())
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With