typescript / intermediate
Snippet
Streaming Data Streams with Typed Async Generators
Async generators typed with AsyncGenerator<T, ReturnType, NextType> allow processing asynchronous sequences lazily. Consuming batches sequentially with for await...of keeps memory consumption low during large data processing.
snippet.ts
typescript
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
interface DataBatch {readonly batchId: number;readonly items: string[];}async function* streamDataBatches(totalBatches: number): AsyncGenerator<DataBatch, void, unknown> {for (let i = 1; i <= totalBatches; i++) {await new Promise(resolve => setTimeout(resolve, 10));yield { batchId: i, items: [`item_${i}A`, `item_${i}B`] };}}async function processStream(): Promise<number> {let totalProcessed = 0;for await (const batch of streamDataBatches(3)) {totalProcessed += batch.items.length;}return totalProcessed;}
Breakdown
1
async function* streamDataBatches(totalBatches: number): AsyncGenerator<DataBatch, void, unknown> {
Declares an async generator function yielding DataBatch items asynchronously.
2
yield { batchId: i, items: [`item_${i}A`, `item_${i}B`] };
Yields a data chunk back to the consumer while pausing generator execution.
3
for await (const batch of streamDataBatches(3)) {
Iterates asynchronously over each emitted batch as soon as it becomes available.
4
totalProcessed += batch.items.length;
Accumulates results in real time without buffering all batches in memory at once.