JavaScript

Looping Through Async/Await

Two situations.

1. Firing all requests at once

async () => {
const promiseArr = [1, 2, 3].map(async n => request(n));
const result = await Promise.all(promiseArr);
// do things to result
}

Or,

2. Firing requests in sequel

await (async fn () => {
for (const n of [1, 2, 3]) {
await request(n);
}
})();

Don’t forget to put an await at the beginning of our async function, since it returns a promise too. Otherwise JS will blow straight past with fn‘s execution, not awaiting its finish.

Standard

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.