I’m new to JS and I don’t understand the behaviour I see in the code snippet
const task1 = new Promise((res, rej) => {
setTimeout(() => res('one'), 5000)
})
const task2 = new Promise((res, rej) => {
setTimeout(() => res('two'), 5000)
})
task1
.then((x) => { // first then
return Promise.resolve(task2)
})
.then((res) => {console.log(res)}) // expect to log after 10 seconds, but logs after 5 seconds
async function test() {
try {
const res = await task1;
const res2 = await task2;
console.log(res2); // also logs after 5 seconds
} catch (error) {
console.error(`error`);
}
}
test();
My understanding is that the first ".then" only fires after task 1 resolves after 5 seconds,then task2 starts and resolves after another 5 seconds. However, the console logs after 5 seconds, not 10. Where have I gone wrong ?
2
Answers
You need to create the second promise in the
then
solving block of the first (promises start executing as soon as they’re created). To fix your code so they sequentially execute as promises:If you want to use async/await with promises:
By creating promises as
new Promise
you execute them immediately so they are independent and doesn’t form any type of queue.To solve that you should have your tasks as functions and call them in a queue (when a promise returned by a function is resolved, call the next function).
To run the next function you should call it inside
.then()
of the previous one.To make things easier you could use an async generator function to convert your array of promise creating functions into an async generator and use
for await
to iterate your promises in a queue.