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

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

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 Denunciar

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 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