Skip to main content
An asynchronous generator method is a class or object literal member prefixed with both 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-in AsyncGenerator<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 to void or any).
  • TNext: The type of the value accepted by the next() method (defaults to unknown).
Alternatively, you can type the return signature using AsyncIterable<TYield> or AsyncIterableIterator<TYield> if you do not need to strictly type the return or next values.

Execution Mechanics

  1. Initialization: Calling the method instantiates the AsyncGenerator. No code inside the method executes until .next() is called.
  2. Promise Resolution: Every yield expression pauses execution. The yielded value is automatically wrapped in a Promise if it is not already one.
  3. IteratorResult: The .next() method returns a Promise that resolves to an IteratorResult object containing two properties:
    • value: The yielded or returned value.
    • done: A boolean indicating whether the generator has completed (true on return, false on yield).

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.
Tired of Poor TypeScript Skills? Fix That With Deep Grasping!Learn More