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
Calling a promise.all with a function using dynamically generated parameters

Hello I am trying to run the same function asynchronously using different parameters but cannot figure out how to store the functions without running them before the promise all.

Here's a simplified example of what I am trying to do:

const myFunc = async(paramsAsArray) => {
    // does stuff
}

let paramArray = [ [a,b], [c, d], [e, f] ]
let funcArray = []

for (let i = 0; i < paramArray.length; i++) {

    funcArray.push(myFunc(paramArray[i]))

}

const response = await Promise.all(funcArray)

My functions keep running in the for loop before I can use the promise.all(). Does anyone know what I can do to make them run a using a promise all? Any help or suggestions is appreciated, thanks!

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

0

myFunc(paramArray[i]) calls the function immediately. You are calling it, so you are not pushing the function itself to the array, but its returned value. Also, the Promise resolution starts immediately, not in the Promise.all.

To push the function itself without calling it, and still passing it parmameters, use .bind :

funcArray.push(myFunc.bind(this, paramArray[i]))

You can also build your array using .map() :

const funcArray = paramArray.map( params => myFunc.bind(this, params));
about 4 years ago · Juan Pablo Isaza Relatório

0

Wrap your function in another function so it doesn't get called immediately. then call it inside Promise.all

funcArray.push(() => myFunc(paramArray[i]))`

const response = await Promise.all(funcArray.map(f=> f()))
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