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

110
Visualizações
Javascript push and wait for async promises in array

I want to wait for an array of Promises but I cannot add them without executing them. So I had to create a wrapper function, however Promise.all does not execute them.

This is my code:

    const set = new Set(Array.from([1, 2, 3, 4, 5]));

    const func = async (input) => {
        let result = await asyncFunc(input);
        return result;
    }

    var promises = [];
    for(const element of set) {
        promises.push(() => { func(element) });
    }

    await Promise.all(promises).then(values => {
        console.log(values);
    }); 

I exepect Promise.all executes the func function, but it does not. How can I achieve this result?

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

0

You could just push promises to your array, not functions:

    const set = new Set(Array.from([1, 2, 3, 4, 5]));

    const func = async (input) => {
        let result = await asyncFunc(input);
        return result;
    }

    var promises = [];
    for(const element of set) {
        promises.push(func(element));
    }

    await Promise.all(promises).then(values => {
        console.log(values);
    }); 
about 4 years ago · Juan Pablo Isaza Relatório

0

I found my problem!

The issue of mine was that some of my promises will reject and Promise.all does not continue when this is the case!

I solved by using Promise.allSettled!

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