🔄 JavaScript Async Iterators: Making Iterators Work in Async Callbacks
The Problem
Imagine you’re reading a book (an iterator) and turning pages one by one. Normally, you can just flip through quickly. But now imagine each page takes time to arrive—like a librarian bringing them one by one with a delay. You can’t just rush through; you have to wait for each page.
That’s what happens when you try to use iterators inside async functions: you need to make them work with asynchronous behavior.
Iterators vs Async Iterators
- A normal iterator: gives you items immediately (like flipping through a finished book).
- An async iterator: gives you items one at a time, but you might have to wait (like the librarian bringing pages).
So, when you’re inside an async callback function, you need to use async iteration tools to handle things properly.
How Do We Do It?
JavaScript gives us a special loop:
for await (let item of asyncIterator) {
console.log(item);
}
This allows you to use an iterator inside async code safely.
Example
Imagine we’re downloading chunks of data:
async function* fetchData() {
yield "First chunk";
await new Promise(r => setTimeout(r, 1000));
yield "Second chunk";
await new Promise(r => setTimeout(r, 1000));
yield "Third chunk";
}
async function process() {
for await (let piece of fetchData()) {
console.log("Got:", piece);
}
}
process();
Output:
Got: First chunk
(wait 1 second)
Got: Second chunk
(wait 1 second)
Got: Third chunk
Here’s what happens:
- We define an async generator (
fetchData
). - Inside
process()
, we usefor await...of
to consume it piece by piece. - Even though each chunk takes time, our code looks clean and readable.
Real-Life Analogy
Think of Netflix 🍿.
- With a normal iterator, you’d have the whole series downloaded before you start watching.
- With an async iterator, Netflix streams you episode by episode—you watch while the rest loads in the background.
When your “callback” (like pressing Play) runs, it doesn’t need the entire show—it just waits for the next episode and continues.
In Short
- Iterators give immediate items; async iterators let you wait for each item.
- Inside async callbacks, we use
for await...of
to handle them properly. - This is perfect for streams, downloads, or any “piece by piece” async task.
âś… Review Questions:
- A normal iterator provides items __________.
- An async iterator provides items that may take __________ to arrive.
- To use iterators inside async code, we use the special loop __________.
- The keyword used to produce values inside an async generator is __________.
- The
await
keyword makes JavaScript __________ until data is ready. - Using
for await...of
makes async code look clean and __________. - A real-life analogy for async iterators is streaming episodes on __________.
- A normal iterator is like flipping through a __________ that’s already complete.
- Async iterators are useful for handling __________ of data (like video or file downloads).
- Iterators in async callbacks prevent the program from __________ while waiting.