Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a promise chain in a for loop

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();
like image 879
TLP Avatar asked Jul 06 '17 17:07

TLP


People also ask

How do you put a promise inside a for loop?

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.

Can promise be chained?

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 ).

How do you run a promise in a sequence?

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.

What is promise chaining give an example?

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.


2 Answers

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.

like image 165
Felix Kling Avatar answered Oct 12 '22 12:10

Felix Kling


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()
    );
}
like image 1
Smiley1000 Avatar answered Oct 12 '22 13:10

Smiley1000