Skip to main content

JavaScript - Asynchronous Iteration


for-await-of#

This is an extremely useful feature. Basically it allows us to create loops of async code with ease!

This feature adds a new “for-await-of” loop that allows us to call async functions that return promises (or Arrays with a bunch of promises) in a loop. The cool thing is that the loop waits for each Promise to resolve before doing to the next loop.

const promises = [
new Promise((resolve) => resolve(1)),
new Promise((resolve) => resolve(2)),
new Promise((resolve) => resolve(3)),
];
// BEFORE
// Runs synchronize
async function beforeWait() {
for (const obj of promises) {
console.log(obj); // // logs 3 promises
}
}
// After
// Waits for promises to finish
async function afterWait() {
for await (const obj of promises) {
console.log(obj); // log
}
}
beforeWait(); // logs 3 promises
afterWait(); // logs 1,2,3
Last updated on