How to wait a Promise inside a forEach loop

我只是一个虾纸丫 提交于 2021-01-29 00:58:36

问题


I'm using some Promise() functions to fetch some data and I got stuck with this problem on a project.

example1 = () => new Promise(function(resolve, reject) {
  setTimeout(function() {
    resolve('foo1');
  }, 3000);
});

example2 = () => new Promise(function(resolve, reject) {
  setTimeout(function() {
    resolve('foo2');
  }, 3000);
});

doStuff = () =>  {
  const listExample = ['a','b','c'];
  let s = "";
  listExample.forEach((item,index) => {
    console.log(item);
    example1().then(() => {
        console.log("Fisrt");
        s = item;
    });
    example2().then(() => {
        console.log("Second");
    });
  });
  console.log("The End");
};

If I call the doStuff function on my code the result is not correct, the result I expected is shown bellow.

RESULT                EXPECTED
a                     a
b                     First
c                     Second
The End               b
Fisrt                 First
Second                Second
Fisrt                 c
Second                First
Fisrt                 Second
Second                The End

At the end of the function no matter how I try, the variable s gets returned as "", I expected s to be "c".


回答1:


It sounds like you want to wait for each Promise to resolve before initializing the next: you can do this by awaiting each of the Promises inside an async function (and you'll have to use a standard for loop to asynchronously iterate with await):

const example1 = () => new Promise(function(resolve, reject) {
  setTimeout(function() {
    resolve('foo1');
  }, 500);
});

const example2 = () => new Promise(function(resolve, reject) {
  setTimeout(function() {
    resolve('foo2');
  }, 500);
});

const doStuff = async () =>  {
  const listExample = ['a','b','c'];
  for (let i = 0; i < listExample.length; i++) {
    console.log(listExample[i]);
    await example1();
    const s = listExample[i];
    console.log("Fisrt");
    await example2();
    console.log("Second");
  }
  console.log("The End");
};

doStuff();

await is only syntax sugar for Promises - it's possible (just a lot harder to read at a glance) to re-write this without async/await:

const example1 = () => new Promise(function(resolve, reject) {
  setTimeout(function() {
    resolve('foo1');
  }, 500);
});

const example2 = () => new Promise(function(resolve, reject) {
  setTimeout(function() {
    resolve('foo2');
  }, 500);
});

const doStuff = () =>  {
  const listExample = ['a','b','c'];
  return listExample.reduce((lastPromise, item) => (
    lastPromise
      .then(() => console.log(item))
      .then(example1)
      .then(() => console.log("Fisrt"))
      .then(example2)
      .then(() => console.log('Second'))
  ), Promise.resolve())
    .then(() => console.log("The End"));
};

doStuff();



回答2:


If you want NOT to wait for each promise to finish before starting the next;

You can use Promise.all() to run something after all your promises have resolved;

example1 = () => new Promise(function(resolve, reject) {
  setTimeout(function() {
    resolve('foo1');
  }, 3000);
});

example2 = () => new Promise(function(resolve, reject) {
  setTimeout(function() {
    resolve('foo2');
  }, 3000);
});

doStuff = () => {
  const listExample = ['a','b','c'];
  let s = "";
  let promises = []; // hold all the promises
  listExample.forEach((item,index) => {
    s = item; //moved
    promises.push(example1() //add each promise to the array
    .then(() => {  
        console.log(item); //moved
        console.log("First");
    }));
    promises.push(example2() //add each promise to the array
    .then(() => {
        console.log("Second");
    }));
  });
  Promise.all(promises) //wait for all the promises to finish (returns a promise)
  .then(() => console.log("The End")); 
  return s;
};
doStuff();


来源:https://stackoverflow.com/questions/53892863/how-to-wait-a-promise-inside-a-foreach-loop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!