Awaiting the Result of Multiple Async Operations | Task

Ole Ersoy
May - 02  -  1 min

Scenario

We have an array of input id data values where each value will be used to fetch some data via an asynchrounous operation that returns a promise.

const ids = [1,2,3]
ids.map(id => fetch(id));
next();

And we want to wait for the promises to resolve before calling next().

Approach

First await Promise.all like this:

async function resolvePromisesAndCallNext(ids) {
    await Promise.all(ids.map(id => fetch(id)));
    next();
}