Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

75
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda