0

I need to use the for-of loop and need to await till all the async requests from looping the inputArray array are done and then return the requests array. How can I make this happen?

const getRequests = async () => {
    const bulkEmails = new BulkEmails();
    const requests = [];
    const inputArray = [];
    for (let input of inputArray) {
        const response = await getAsyncData(input);
        if (response.data) {
            bulkEmails.addEmail(..email); // Email added
            if (bulkEmails.emails.length > 500) {
               // If more than 500 emails in one bulk email object, push object with data to requests array
               requests.push({bulkEmails, data: response.data});
               bulkEmails = [];
            }
        }
    }
    return requests;
}

const requests = await getRequests();
// Loop over all (prepare requests to finally send them
Robbert
  • 1,270
  • 6
  • 30
  • 62
  • "*I need to use the for-of loop and need to await till all the async requests from looping the inputArray array are done and then return the requests array.*" that's what your code already does, isn't it? It's done sequentially but `await getRequests();` should return an array with all the data. – VLAZ Mar 08 '22 at 16:36
  • It would be better if you use 'promise.all', follow this article https://hackmamba.io/blog/2020/12/aggregate-multiple-api-requests-with-promise-all/ – Ahsan Ali Mar 08 '22 at 16:38
  • @AhsanAli but I cannot use promise all with for-of right? – Robbert Mar 08 '22 at 16:39
  • You could use `Promise.all()` *instead* of `for...of` to wait for all in parallel instead of sequentially: [Using async/await with a forEach loop](https://stackoverflow.com/q/37576685) | [Use async await with Array.map](https://stackoverflow.com/q/40140149) – VLAZ Mar 08 '22 at 16:40
  • @VLAZ but then I need to use .map() and not every iteration returns something + in the real version there is more logic with loopts inside it. – Robbert Mar 08 '22 at 16:42
  • You can filter the resulting array. If each iteration is independent of the others, then there is no real reason to do them sequentially. It would just take more time in the end. – VLAZ Mar 08 '22 at 16:43
  • @VLAZ but the iteration adds elements to another array as well which is used in the next iteration. Then .map() is not the best solution right? – Robbert Mar 08 '22 at 16:47
  • What next iteration? What you've shown has each async task independent. If you need to iterate *twice* over some async data, you can still do `const results = Promise.all(myPromises); const dataWanted = results.filter(x => /*some criteria*/; for (const item of dataWanted) {}` if you mean that there are relations *while* iterating like using the data from the last iteration, then no - in that case, waiting in parallel is not correct. – VLAZ Mar 08 '22 at 16:52
  • @VLAZ I tried to create a simulation of the real version. Hope you understand what I want to achieve and why I chose for of loop in the first place. – Robbert Mar 08 '22 at 16:57
  • 1
    Can you come back to the *problem* you have with the current code? What is not working? – trincot Mar 08 '22 at 17:06
  • @trincot I cannot await till the requests are async done before returning. – Robbert Mar 10 '22 at 07:51
  • 1
    This `getRequests` function is awaiting each call of `getAsyncData` before getting to the function's `return` statement, so I don't know what you are referring to. Of course, `getAsyncData` needs to return a promise which only resolves when the async task is done. If that is not the case, you'll indeed have an issue. But you didn't provide its code, so we must assume it works fine. – trincot Mar 10 '22 at 07:55

0 Answers0