An asynchronous generator method is a class or object literal member prefixed with bothDocumentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
async and *. It combines the pause-and-resume execution model of generators with the non-blocking, promise-based resolution of asynchronous functions. When invoked, it does not execute its body immediately; instead, it returns an AsyncGenerator object. The generator yields values of type TYield, and it is the .next() method of the resulting iterator that returns a Promise<IteratorResult<TYield>>.
TypeScript Signatures and Typing
In TypeScript, the return type of an async generator method is strictly typed using the built-inAsyncGenerator<TYield, TReturn, TNext> interface:
TYield: The type of the values yielded by the method.TReturn: The type of the value returned when the generator completes (defaults tovoidorany).TNext: The type of the value accepted by thenext()method (defaults tounknown).
AsyncIterable<TYield> or AsyncIterableIterator<TYield> if you do not need to strictly type the return or next values.
Execution Mechanics
- Initialization: Calling the method instantiates the
AsyncGenerator. No code inside the method executes until.next()is called. - Promise Resolution: Every
yieldexpression pauses execution. The yielded value is automatically wrapped in a Promise if it is not already one. - IteratorResult: The
.next()method returns aPromisethat resolves to anIteratorResultobject containing two properties:value: The yielded or returned value.done: A boolean indicating whether the generator has completed (trueonreturn,falseonyield).
Delegation with yield*
An async generator method can delegate its execution to another AsyncIterable, Iterable, or generator using the yield* expression. TypeScript enforces that the delegated iterable’s yield type is compatible with the parent method’s TYield type.
Consumption via for await...of
While manual .next() calls expose the underlying Promise mechanics, async generator methods are natively designed to be consumed by the for await...of statement. This construct implicitly awaits each Promise and extracts the value from the IteratorResult, automatically terminating when done: true is encountered.
Master TypeScript with Deep Grasping Methodology!Learn More





