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

188
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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