typescript / intermediate
Snippet
Consuming Paginated Data Streams with Async Generators
Async Generators combine asynchronous control flow with generators using the async function* syntax, yielding promises sequentially to consume stream-like data efficiently without loading all batches into memory.
snippet.ts
typescript
1
2
3
4
5
6
7
8
9
10
11
12
13
14
async function* fetchPaginatedRecords(limit: number): AsyncGenerator<number[], void, unknown> {let page = 1;while (page <= 3) {const records = Array.from({ length: limit }, (_, i) => (page - 1) * limit + i + 1);yield records;page++;}}(async () => {for await (const batch of fetchPaginatedRecords(2)) {console.log('Batch:', batch);}})();
Breakdown
1
async function* fetchPaginatedRecords(limit: number): AsyncGenerator<number[], void, unknown> {
Declares an async generator returning an AsyncGenerator type yielding arrays of numbers.
2
yield records;
Yields the current batch of records asynchronously to the consumer loop.
3
for await (const batch of fetchPaginatedRecords(2)) {
Uses the for await...of loop to pause execution until each yielded batch resolves.