0

The problem is when i set the value in the array from the each promise, the value not return when forEach finish in the array.

let works = [];
    const imgsUrl = saveImages(imgs);
    imgsUrl.map(async (img) => {
        img.then(value => {
            works = [...works, value];
        })

    });

Someone any idea, thanks by the way.

  • Welcome to SO! This isn't quite a [mcve] so it's hard to infer your intent, but are you looking for [`Promise.all`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all) by chance? – ggorlen Apr 07 '21 at 23:52
  • 1
    Does this answer your question? [Using async/await with a forEach loop](https://stackoverflow.com/questions/37576685/using-async-await-with-a-foreach-loop) (you're (ab)using `map` as essentially a `forEach` here) – ggorlen Apr 07 '21 at 23:53
  • …and also don't mix `then` with `await`. – Bergi Apr 08 '21 at 00:01
  • You might be looking for `const works = (await Promise.all(saveImages(imgs))).flat()`, but really the `saveImages` function should do the `Promise.all` already and return a promise for an array, not an array of promises. – Bergi Apr 08 '21 at 00:02
  • Thanks, Promise.all help me to return an array data – Andres97 Apr 08 '21 at 00:54

0 Answers0