Does the for await
loop wait for the first iteration to be done before calling the next promise?
The first snippet uses Promise.all
to fire all the promises at the same time:
const promises = await Promise.all(items.map(e => somethingAsync(e)));
for (const res of promises) {
// do some calculations
}
The second snippet uses for await
to loop over the promises, and waits for each iteration to finish before calling the next one:
for await (const res of items.map(e => somethingAsync(e))) {
// do some calculations
}
Therefore, the answer to the question is yes, the for await
loop waits for the first iteration to be done before calling the next promise.