3

I'm working with javascript and I want that wait for other function response before iterate next item.

Here is the desired behaviour:

enter image description here
This is my code:

let lista = [1,2,3,4]
console.log('Iteracion de la lista')
async function procesarLista(array){
    for(const item of array){
        console.log('-->START indice: ' + item)
        //Simulate delay (for each iteration) of backend response
        setTimeout(function(){
            console.log('....waiting.... for : ' + item );
        }, 2500);
        console.log('-->FINISH indice: ' + item)
    }
    console.log('Done');
 }
 //Execute:
 procesarLista(lista);

This is the WRONG result:

enter image description here

3 Answers 3

1

Try awaiting a Promise on each iteration inside the for loop:

let lista = [1, 2, 3, 4]
console.log('Iteracion de la lista')
async function procesarLista(array) {
  for (const item of array) {
    await new Promise((resolve) => {
      console.log('-->START indice: ' + item)
      //Simulate delay (for each iteration) of backend response
      setTimeout(function() {
        console.log('....waiting.... for : ' + item);
        resolve();
      }, 500);
    });
    console.log('-->FINISH indice: ' + item)
  }
  console.log('Done');
}
//Execute:
procesarLista(lista);

for..of requires regenerator-runtime. If you don't have that, then you can use reduce instead, but you'll have to await the last iteration's resolution inside the loop and await the final promise before logging Done:

let lista = [1, 2, 3, 4]
console.log('Iteracion de la lista')
async function procesarLista(array) {
  await new Promise ((outerResolve) => {
    array.reduce(async (lastPromise, item) => {
      await lastPromise;
      await new Promise((resolve) => {
        console.log('-->START indice: ' + item)
        //Simulate delay (for each iteration) of backend response
        setTimeout(function() {
          console.log('....waiting.... for : ' + item);
          resolve();
        }, 500);
      });
      console.log('-->FINISH indice: ' + item)
    }, Promise.resolve());
  });
  console.log('Done');
}
procesarLista(lista);

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, but I have another error message: regeneratorRuntime is not defined
See edit - if you can't use for..of, then if you still want to use async/await, then use .reduce.
0

Maybe this would work for you

lista = [1, 2, 3, 4];

async function procesarItem(array, id) {
  if (id >= array.length) {
    console.log('Done');
    return;
  }
  console.log('-->START indice: ' + id);
  setTimeout(function () {
    console.log('-->FINISH indice: ' + id)
    procesarItem(array, id + 1);
  }, 2500);
  console.log('....waiting.... for : ' + id);
}

//Execute:
procesarItem(lista, 0);

Comments

0

You can use callback as well :

let lista = [1,2,3,4]
console.log('Iteracion de la lista')
function procesarLista(array , callback){
  let isProcessDone = false;
  for(const item of array){

    //Simulate delay (for each iteration) of backend response
    setTimeout(function(){
            console.log('-->START indice: ' + item)
        console.log('....waiting.... for : ' + item );
        console.log('-->FINISH indice: ' + item)
        callback('Done')
    }, 2500);

  }
}
//Execute:
procesarLista(lista , function(result){
   // Do Something after each job is done
});

this is going to be the output :

Iteracion de la lista
-->START indice: 1
 ....waiting.... for : 1
-->FINISH indice: 1
-->START indice: 2
 ....waiting.... for : 2
-->FINISH indice: 2
-->START indice: 3
 ....waiting.... for : 3
-->FINISH indice: 3
-->START indice: 4
 ....waiting.... for : 4
-->FINISH indice: 4

here is an example :

https://jsfiddle.net/op98bpka/1/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.