> ## Documentation Index
> Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
> Use this file to discover all available pages before exploring further.

# TypeScript Async Generator Method

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.

```typescript theme={"dark"}
class StreamHandler {
    // Explicitly typing Yield, Return, and Next types
    public async *extractData(): AsyncGenerator<Uint8Array, number, string> {
        // 'command' is typed as 'string' based on TNext
        const command: string = yield new Uint8Array([1, 2, 3]);
        
        if (command === "STOP") {
            return 0; // Typed as 'number' based on TReturn
        }
        
        yield new Uint8Array([4, 5, 6]);
        return 1;
    }
}

// Object literal syntax
const asyncIterableObj = {
    async *generateSequence(): AsyncIterableIterator<number> {
        yield await Promise.resolve(10);
        yield await Promise.resolve(20);
    }
};
```

## 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`).

```typescript theme={"dark"}
const handler = new StreamHandler();
const generator = handler.extractData();

// Manual iteration mechanics
generator.next().then(result => {
    console.log(result); // { value: Uint8Array(3) [1, 2, 3], done: false }
    
    // Passing TNext back into the generator
    return generator.next("STOP"); 
}).then(result => {
    console.log(result); // { value: 0, done: true }
});
```

## 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.

```typescript theme={"dark"}
class CompositeGenerator {
    private async *subRoutine(): AsyncGenerator<string, void, unknown> {
        yield "sub-yield-1";
        yield "sub-yield-2";
    }

    public async *mainRoutine(): AsyncGenerator<string, void, unknown> {
        yield "main-yield-1";
        // Delegates iteration to subRoutine
        yield* this.subRoutine(); 
        yield "main-yield-2";
    }
}
```

## 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.

```typescript theme={"dark"}
async function consume(handler: CompositeGenerator) {
    // TypeScript infers 'item' as 'string' based on TYield
    for await (const item of handler.mainRoutine()) {
        console.log(item);
    }
}
```

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor TypeScript Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
