Home > other >  How can I call multiple asynchronous depended functions asynchronously for each iteration?
How can I call multiple asynchronous depended functions asynchronously for each iteration?

Time:10-18

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.

CodePudding user response:

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)

CodePudding user response:

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);
  • Related