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

260
Visualizações
Why the execution doesn't stop at await (async/await JS)?

I am writing a function that should execute all async functions as fast as possible, however, only 5 of them can run at the same time.

I wanted to do it using Promise.race so the implementation is not the best. The problem is that the code execution doesn't stop at await. I would expect, that counter variable would be decremented only when the promise resolves but it doesn't wait for the promise to resolve. The code doesn't stop at Promise.all either which makes me thing that my understanding of promises is not proper. The code is below and the function in question is runPromises.

async function runPromises(promises, limit) {
  const LIMIT = limit || 10;
  let promsCopy = promises.slice();
  let counter = 0;
  let queue = [];
  while (promsCopy.length !== 0) {
    if (counter < LIMIT) {
      let prom = promsCopy.shift();
      if (prom) {
        queue.push(prom());
        counter += 1;
        if (counter >= LIMIT) {
          let [completed] = await Promise.race(
            queue.map((p) => p.then((_) => [p]))
          );
          queue.splice(queue.indexOf(completed), 1);
          counter -= 1;
        }
      }
    }
  }
  await Promise.all(queue);
}

// the code below is just for testing

const asyncFuncLong = () => {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      resolve();
    }, 6000);
  });
};

const asyncFuncShort = () => {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      resolve();
    }, 1000);
  });
};

let fns = [
  asyncFuncLong,
  asyncFuncLong,
  asyncFuncLong,
  asyncFuncLong,
  asyncFuncLong,
  asyncFuncLong,
  asyncFuncLong,
  asyncFuncLong,
  asyncFuncLong,
  asyncFuncLong,
  asyncFuncLong,
  asyncFuncLong,
  asyncFuncLong,
  asyncFuncLong,
  asyncFuncLong,
  asyncFuncLong,
];

let start = Date.now();

runPromises(fns, 10).then(() => {
  console.log(Date.now() - start);
});


Edit: Fixed. Everything works now. Thank you both!

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

The fns array is a list of functions. Those functions return Promise objects. You are then passing that list of functions to your runPromises function, which appears to want to take a list of Promise objects as an argument. But you are passing it a list of functions.

My suggestions are either

  • change the runPromises function so that it actually calls prom() at some point (or just use a map, like const r = await Promise.all(queue.map((fn) => { return fn(); }));), or

  • change the definition of fns to something like let fns = [ asyncFuncLong(), asyncFuncShort(), asyncFuncLong(), ... ];.

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