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

194
Vistas
How can I call multiple asynchronous depended functions asynchronously for each iteration?

I have some functions,

const firstResult = await firstFunction(parameters)
const secondResult = await secondFunction(firstResult)
const finalResult = await finalFunction(secondResult)

They're asynchronous functions, returning some promise. Now there's a scenario like I have an array of parameters that is getting passed to `firstFunction at each iteration and it should follow the same flow of execution. Something like this:

const results = arrayOfParamters.map(async parameter => {
    const firstResult = await firstFunction(parameters)
    const secondResult = await secondFunction(firstResult)
    const finalResult = await finalFunction(secondResult)
    return finalResult
})

So how can I achieve this more efficiently? One of the efficient solutions that I can visualize is to execute firstFunction for the first parameter, start executing secondFunction and till then the promise for secondFunction is fulfilled, start executing the firstFunction for the next iteration of arrayOfParameters. Is there any way I can achieve this? Seems like Promise.all will not be worth it if the whole loop is going to be executed synchronously.

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Your code is already set up to do that.

It's going to start by running the map code for element 0 of the array. It synchronously calls firstFunction but then it hits an await, and so the mapping function returns a promise. This promise gets put in element 0 of results. Then it does the same thing for element 1, then 2, etc. So it will call firstFunction for every element of the array, and assign an array of promises to results, but none of them will block or wait.

The array of promises has now been created, so the code continues on to whatever comes after this line. Over time, the calls to firstFunction will start finishing, and the async functions will resume, calling secondFunction for each element of the array (not necessarily in exactly the same order, since this is happening at an unpredictable time). And then eventually they'll all do third function, and finally they'll resolve their promises to finalResult.

So the only thing missing from your code is to know when it's all done, and for that you need Promise.all:

const promises = arrayOfParamters.map(async parameter => {
    const firstResult = await firstFunction(parameter)
    const secondResult = await secondFunction(firstResult)
    const finalResult = await finalFunction(secondResult)
    return finalResult
})

const results = await Promise.all(promises)
about 4 years ago · Juan Pablo Isaza Denunciar

0

Because Promise.then chain the result as a parameter to the next function you can use that instead of async await

You can use Promise.all like this:

const promiseChain = arrayOfParamters.map((parameter) => firstFunction(parameter).then(secondFunction).then(finalFunction));
const results = await Promise.all(promiseChain);
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