← Back to TypeScript Course | Chapter 21: Symbols and Iterators | Lesson 5 of 6

Async Generators की जानकारी

Async generators generators को asynchronous operations के साथ combine करते हैं। वे async function* उपयोग करते हैं और AsyncIterator protocol के through values produce करते हैं, जिन्हें for await...of से consume किया जा सकता है।
Syntax
typescript
async function* name(): AsyncGenerator<YieldType> {
  yield value;
}
for await (const item of name()) {
  // use item
}

Basic Async Generator

एक async generator हर yield के बीच asynchronous work await कर सकता है, और इसका return type तीनों value slots के लिए पूरी तरह AsyncGenerator<YieldType, ReturnType, NextType> के रूप में describe किया जा सकता है।

उदाहरण: Basic Async Generator

typescript
async function* countAsync(): AsyncGenerator<number, void, unknown> {
  yield 1;
  yield 2;
}
async function run() {
  for await (const n of countAsync()) console.log(n);
}
run();

Async Data Sequences

Async generators naturally fit होते हैं जब भी किसी sequence के हर item को अपना खुद का asynchronous step चाहिए हो — results का एक page एक समय में fetch करना, या अगली value produce करने से पहले एक remote data source पर wait करना।

उदाहरण: Async Data Sequences

typescript
async function* fetchPages(): AsyncGenerator<string> {
  yield "page 1";
  yield "page 2";
}
async function run() {
  for await (const page of fetchPages()) console.log(page);
}
run();

Async Generator Return Types

AsyncGenerator के type parameters आपको exactly specify करने देते हैं कि क्या yield होता है, generator ultimately क्या return करता है, और next के through वापस कौन सी type की value pass की जा सकती है — एक regular generator जैसा वही three-part contract, सिर्फ asynchronous।

उदाहरण: Async Generator Return Types

typescript
async function* gen(): AsyncGenerator<number, string, unknown> {
  yield 1;
  return "complete";
}
async function run() {
  const it = gen();
  console.log(await it.next());
  console.log(await it.next());
}
run();

for await...of

for await...of loop specifically async iterators consume करने के लिए बना है — यह automatically हर step के result को await करता है, जो इसे async generators और दूसरे async iterables दोनों के साथ naturally काम करने देता है।

उदाहरण: for await...of

typescript
async function* asyncNums(): AsyncGenerator<number> {
  yield 10;
  yield 20;
}
async function run() {
  for await (const n of asyncNums()) {
    console.log(n);
  }
}
run();

Async Generator Pipelines

Async generators को chain किया जा सकता है एक lazy processing pipeline बनाने के लिए, जहां हर stage अगली value सिर्फ तब pull और process करता है जब इसके बाद वाला stage actually इसे मांगता है।

उदाहरण: Async Generator Pipelines

typescript
async function* numbers(): AsyncGenerator<number> {
  yield 1;
  yield 2;
  yield 3;
}
async function* doubled(source: AsyncGenerator<number>): AsyncGenerator<number> {
  for await (const n of source) yield n * 2;
}
async function run() {
  for await (const n of doubled(numbers())) console.log(n);
}
run();
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. एक async generator पर normal for...of उपयोग करना, जब इसे for await...of चाहिए।
  2. async function* लिखना लेकिन इसे Generator के बजाय AsyncGenerator typed न करना।
  3. एक async function (या top-level module) के बाहर for await उपयोग करना, जो एक error है।
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.