For-Await-Of with Promises Arrays
What you'll learn: How for-await-of processes regular arrays containing promises, awaiting each one sequentially as it iterates.
Arrays of Promises Are Also Iterable
You've seen for-await-of work with async iterables (objects with Symbol.asyncIterator), but it also works beautifully with ordinary arrays that contain promises. When you pass an array of promises to for-await-of, it doesn't just hand you the promises themselves—it awaits each promise before giving you its resolved value.
Think of it like opening a series of locked boxes in order. You don't move to the next box until you've successfully opened the current one and retrieved what's inside.
How It Works
const promises = [
Promise.resolve('First'),
Promise.resolve('Second'),
new Promise(resolve => setTimeout(() => resolve('Third'), 100))
];
for await (const value of promises) {
console.log(value);
}
// Logs: "First", "Second", "Third"
// Each promise is awaited before continuing
Sequential Processing
This is crucial: for-await-of processes promises one at a time, in order. Even if later promises could resolve faster, they wait their turn:
const delays = [
new Promise(resolve => setTimeout(() => resolve('Slow (500ms)'), 500)),
Promise.resolve('Fast (instant)')
];
for await (const result of delays) {
console.log(result);
}
// "Slow (500ms)" appears first (after 500ms)
// Only then does "Fast (instant)" appear
This differs from Promise.all(), which starts all promises simultaneously. for-await-of ensures predictable, sequential execution through your array.
Key Takeaway: for-await-of automatically awaits each promise in an array sequentially, letting you write clean iteration code without manual await calls or index management.