Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

73
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda