Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

68
Views
Functions executed without waiting for Promise.all call

I have a multidimensional array of promises, but the execution of executeMethod occurs before the initial for loop ends and the code reaches the second for loop and Promise.all.

executeMethod is of course an async function.

const MAX_NUMBER_OF_CONCURRENT_PROMISES = 100;
const promisesArray: Promise<void>[][] = [];
let promiseArrayIndex = 0;
let innerPromiseArrayIndex = 0;

const objectsList = [......];

for (const bucketObject of objectsList) {
    if (innerPromiseArrayIndex === MAX_NUMBER_OF_CONCURRENT_PROMISES) {
      innerPromiseArrayIndex = 0;
      promiseArrayIndex++;

      promisesArray[promiseArrayIndex] = [];
    }

    promisesArray[promiseArrayIndex][innerPromiseArrayIndex] = (
      executeMethod(bucketObject)
    );
    innerPromiseArrayIndex++;
}

for (let i=0; i< promiseArrayIndex; i++) {
  await Promise.all(promisesArray[i]);
}

I want the executions to occur only after for (const bucketObject of objectsList) ends and I call Promise.all per each Promises array.

Please advise how can I resolve this?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

It's not Promise.all that executes anything, it's the executeMethod(bucketObject) call. And those all happen synchronously in your loop, before you start waiting for anything.

To batch your executions, use

const MAX_NUMBER_OF_CONCURRENT_PROMISES = 100;
const objectsList = [......];

for (let index = 0; index<objectsList.length; index+=MAX_NUMBER_OF_CONCURRENT_PROMISES) {
   const promisesArray: Promise<void>[] = objectsList.slice(index, index+MAX_NUMBER_OF_CONCURRENT_PROMISES).map(executeMethod);
   await Promise.all(promisesArray);
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!