I would expect the code below to print one number on the console, then wait a second and then print another number. Instead, it prints all 10 numbers immediately and then waits ten seconds. What is the correct way to create a promise chain that behaves as described?
function getProm(v) {
return new Promise(resolve => {
console.log(v);
resolve();
})
}
function Wait() {
return new Promise(r => setTimeout(r, 1000))
}
function createChain() {
let a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0];
let chain = Promise.resolve();
for (let i of a) {
chain.then(()=>getProm(i))
.then(Wait)
}
return chain;
}
createChain();
To use Javascript promises in a for loop, use async / await . This waits for each promiseAction to complete before continuing to the next iteration in the loop. In this guide, you learn how async/await works and how it solves the problem of using promises in for loops.
Introduction to the JavaScript promise chainingThe callback passed to the then() method executes once the promise is resolved. In the callback, we show the result of the promise and return a new value multiplied by two ( result*2 ).
The essence of this function is to use reduce starting with an initial value of Promise. resolve([]) , or a promise containing an empty array. This promise will then be passed into the reduce method as promise . This is the key to chaining each promise together sequentially.
Example 2: Chaining the Promise with then() Promise resolved You can call multiple functions this way. In the above program, the then() method is used to chain the functions to the promise. The then() method is called when the promise is resolved successfully. You can chain multiple then() methods with the promise.
You have to assign the return value of .then
back to chain
:
chain = chain.then(()=>getProm(i))
.then(Wait)
Now you will basically be doing
chain
.then(()=>getProm(1))
.then(Wait)
.then(()=>getProm(2))
.then(Wait)
.then(()=>getProm(3))
.then(Wait)
// ...
instead of
chain
.then(()=>getProm(1))
.then(Wait)
chain
.then(()=>getProm(2))
.then(Wait)
chain
.then(()=>getProm(3))
.then(Wait)
// ...
You can see that the first one is actually a chain, while the second one is parallel.
See Resolve promises one after another (i.e. in sequence)?.
Using i of a
:
function createChain() {
let a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0];
let chain = Promise.resolve();
for (let i of a) {
chain = chain.then(()=>getProm(i))
.then(Wait)
}
return chain;
}
Using a.forEach()
function createChain() {
let a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0];
let chain = Promise.resolve();
a.forEach(i =>
chain = chain.then(()=>getProm(i))
.then(Wait)
);
return chain;
}
Using a.reduce()
function createChain() {
let a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0];
return a.reduce((chain, i) =>
chain.then(()=>getProm(i))
.then(Wait),
Promise.resolve()
);
}
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