typescript / intermediate
Snippet
Streaming Data Batches with Async Generators and Async Iterators
Async generator functions combine the yield keyword with asynchronous operations. Returning an AsyncGenerator interface allows consumers to process paginated data sequentially item-by-item using 'for await...of' loops without loading all records into memory at once.
snippet.ts
typescript
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
interface DataPage<T> {items: T[];nextCursor?: string;}async function* fetchPagedData<T>(fetcher: (cursor?: string) => Promise<DataPage<T>>): AsyncGenerator<T, void, unknown> {let cursor: string | undefined = undefined;do {const page: DataPage<T> = await fetcher(cursor);for (const item of page.items) {yield item;}cursor = page.nextCursor;} while (cursor !== undefined);}
Breakdown
1
async function* fetchPagedData<T>(
Declares an asynchronous generator function returning values of generic type T.
2
yield item;
Emits individual items one at a time to the async iterator caller.
3
} while (cursor !== undefined);
Continues fetching subsequent pages as long as a valid cursor is provided by the previous page payload.