Docs
16.5-Async-Iterators-Streams
18.5 Async Iterators & Streams
Overview
Async iterators and streams allow processing of data that arrives over time, perfect for handling large datasets, real-time data, or paginated APIs.
Async Iteration
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Sync vs Async Iteration β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β β
β SYNC ITERATION: β
β Data is immediately available β
β β
β for (const item of array) { β
β console.log(item); β
β } β
β β
β ASYNC ITERATION: β
β Data arrives over time β
β β
β for await (const item of asyncSource) { β
β console.log(item); β
β } β
β β
β Timeline: β
β ββββ¬βββββββ¬βββββββ¬βββββββ¬βββββββ¬ββββΆ time β
β β β β β β β
β item 1 item 2 item 3 item 4 done β
β β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
Async Iterator Protocol
| Method | Returns | Description |
|---|---|---|
[Symbol.asyncIterator]() | Async Iterator | Returns the async iterator |
next() | Promise<{value, done}> | Returns next value |
return() (optional) | Promise<{done: true}> | Cleanup on early exit |
throw() (optional) | Promise | Handle thrown errors |
Creating Async Iterables
1. Async Generator Function
async function* fetchPages(url) {
let page = 1;
while (true) {
const data = await fetch(`${url}?page=${page}`);
const items = await data.json();
if (items.length === 0) break;
yield* items;
page++;
}
}
2. Custom Async Iterable
const asyncIterable = {
async *[Symbol.asyncIterator]() {
yield await fetchItem(1);
yield await fetchItem(2);
yield await fetchItem(3);
},
};
Stream Concepts
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Data Stream Flow β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β β
β Source βββΆ Transform βββΆ Transform βββΆ Sink β
β β β β β β
β produce process process consume β
β β
β Examples: β
β ββββββββββββ βββββββββββ βββββββββββ ββββββββββ β
β β API Data ββββΆβ Parse ββββΆβ Filter ββββΆβ Displayβ β
β ββββββββββββ βββββββββββ βββββββββββ ββββββββββ β
β β
β ββββββββββββ βββββββββββ βββββββββββ ββββββββββ β
β β File ββββΆβDecompressβββΆβTransformββββΆβ Save β β
β ββββββββββββ βββββββββββ βββββββββββ ββββββββββ β
β β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
Web Streams API
| Stream Type | Purpose | Method |
|---|---|---|
| ReadableStream | Source of data | getReader() |
| WritableStream | Destination | getWriter() |
| TransformStream | Process data | readable, writable |
Using for await...of
// With async generator
for await (const item of asyncGenerator()) {
console.log(item);
}
// With readable stream
const reader = stream.getReader();
for await (const chunk of reader) {
processChunk(chunk);
}
Backpressure
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Backpressure Control β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β β
β WITHOUT BACKPRESSURE: β
β Producer: βββββββββββββββββββββββββΆ Fast β
β Consumer: ββββββββββΆ Slow β
β Buffer: βββββββββββββββββββββ β OVERFLOW! β
β β
β WITH BACKPRESSURE: β
β Producer: βββββββββββββΆ Slowed down β
β Consumer: ββββββββββΆ At its pace β
β Buffer: βββββββ β Controlled β
β β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
Summary
- β’Use async iterators for data over time
- β’
for await...ofsimplifies consumption - β’Web Streams for large data processing
- β’Handle backpressure to prevent memory issues
- β’Combine with generators for powerful patterns