Streaming asynchronous data
Read paginated or streamed data without loading all results at once.
TypeScript · Interface
AsyncGenerator is the iterator type produced by an async generator function. It yields values asynchronously and exposes promise-based iteration operations.
AsyncGenerator describes the object returned by an async generator function. Its methods asynchronously report iteration results through Promises, while the generic parameters represent yielded values, the final return value, and values accepted by next().
interface AsyncGenerator<T = unknown, TReturn = any, TNext = unknown> extends AsyncIterator<T, TReturn, TNext> {}An AsyncGenerator object that asynchronously produces iteration results.
async function* numbers(): AsyncGenerator<number, void, unknown> {
yield 1;
yield 2;
}
for await (const number of numbers()) {
console.log(number);
}The async generator produces values that can be consumed asynchronously with for await...of.
Read paginated or streamed data without loading all results at once.
Expose incremental results from an asynchronous computation.
Consume asynchronous sources with standard iteration syntax.
AsyncGenerator is the iterator type produced by an async generator. Unlike a synchronous iterator, its next, return, and throw operations produce promises for iteration results.
Follow-up: How does it differ from AsyncIterator? AsyncGenerator is the specialized interface for generator-produced async iterators and includes generator behavior in addition to asynchronous iteration.
Its type parameters describe the yielded value, the generator's eventual return value, and the value accepted by next(). A declaration such as AsyncGenerator<number, void, unknown> yields numbers, returns void, and accepts an unknown input.
Follow-up: Why use unknown for the third parameter? It safely represents an input whose type is not constrained by the generator implementation.
An async generator is declared with async function*. It can yield partial results over time, and consumers can process those results with for await...of.
Follow-up: Can async generators delegate with yield*? Yes; the documentation says delegation can target either an iterable or an async iterable.
AsyncIteratorIteratorResultSymbol.asyncIteratorfor..await..of