JS जनरेटर्स
In this page:
function* generatorName() {
yield value1;
yield value2;
}
const generator = generatorName();
generator.next();
Generator क्या है?
एक generator एक खास function है जो रुक सकता है और बाद में जारी रह सकता है। इसे एक asterisk से declare किया जाता है। एक normal function के उलट जो पूरा होने तक चलता है, generator की execution को function के बीच में suspend किया जा सकता है और ठीक वहीं से resume किया जा सकता है जहाँ वह छोड़ा गया था।
उदाहरण: What Is a Generator?
// Define the generator function `numberGenerator` with no parameters
// Define the generator function `numberGenerator` with no parameters
function* numberGenerator() {
yield 1;
yield 2;
}
// Declare the constant `gen`, set to `numberGenerator()`
// Declare the constant `gen`, set to `numberGenerator()`
const gen = numberGenerator();
// Print `gen.next()` to the console
// Print `gen.next()` to the console
console.log(gen.next());
yield
yield किसी generator को रोक देता है और caller को एक value भेजता है। next() method उसी point से जारी रहता है।
gen.next() कॉल करना generator को अगले yield (या अंत) तक चलाता है, { value, done } return करते हुए — यह generators को values का एक controllable, step-by-step source बनाता है।
उदाहरण: yield
function* counter() {
yield 1;
yield 2;
}
const gen = counter();
console.log(gen.next()); // { value: 1, done: false }
console.log(gen.next()); // { value: 2, done: false }
console.log(gen.next()); // { value: undefined, done: true }
Generator Iteration
Generators iterable होते हैं। एक for...of loop उनकी yielded values पढ़ सकता है। इसका मतलब है कि आप किसी generator के output को उसी तरह iterate कर सकते हैं जैसे किसी array को, बिना पहले हर value को memory में इकट्ठा किए।
उदाहरण: Generator Iteration
// Define the generator function `colors` with no parameters
// Define the generator function `colors` with no parameters
function* colors() {
yield "red";
yield "green";
}
// Loop over `colors()`, binding each item to `color`
// Loop over `colors()`, binding each item to `color`
for (const color of colors()) {
// Print `color` to the console
// Print `color` to the console
console.log(color);
}
Values भेजना
next() किसी paused generator में वापस एक value भेज सकता है। यह generators को caller के साथ communicate करने देता है।
आप next(value) में भी एक value पास कर सकते हैं, जो generator के अंदर paused yield expression का result बन जाती है, जो two-way communication संभव बनाता है।
उदाहरण: Sending Values
// Define the generator function `echo` with no parameters
// Define the generator function `echo` with no parameters
function* echo() {
// Declare the constant `received`, set to `yield "ready"`
// Declare the constant `received`, set to `yield "ready"`
const received = yield "ready";
// Print `"Got:", received` to the console
// Print `"Got:", received` to the console
console.log("Got:", received);
}
// Declare the constant `gen`, set to `echo()`
// Declare the constant `gen`, set to `echo()`
const gen = echo();
// Print `gen.next()` to the console
// Print `gen.next()` to the console
console.log(gen.next());
// Call `gen.next("hello")`
// Call `gen.next("hello")`
gen.next("hello");
Practical इस्तेमाल
Generators controlled sequences, custom iterables, और lazy data processing के लिए उपयोगी हैं।
चूंकि एक generator हर value सिर्फ पूछे जाने पर calculate करता है, यह infinite sequences या महंगी computations के लिए एक स्वाभाविक विकल्प है जिन्हें आप सिर्फ उतनी दूर तक चलाना चाहते हैं जितनी असल में ज़रूरत हो।
उदाहरण: Practical Use
// Define the generator function `infiniteCount` with no parameters
// Define the generator function `infiniteCount` with no parameters
function* infiniteCount() {
// Declare the variable `n`, set to `1`
// Declare the variable `n`, set to `1`
let n = 1;
while (true) yield n++;
}
// Declare the constant `gen`, set to `infiniteCount()`
// Declare the constant `gen`, set to `infiniteCount()`
const gen = infiniteCount();
// Print `gen.next().value, gen.next().value, gen.next().value` to the console
// Print `gen.next().value, gen.next().value, gen.next().value` to the console
console.log(gen.next().value, gen.next().value, gen.next().value);
- किसी generator function को कॉल करना और उसके चलने की उम्मीद करना, जबकि यह सिर्फ एक generator object return करता है जब तक आप
next()कॉल न करें। - किसी normal function या callback के अंदर
yieldइस्तेमाल करना, जो एकSyntaxErrorहै। - किसी generator पर दूसरी बार loop करना और कुछ न मिलना, क्योंकि यह पहले से खत्म हो चुका है।
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: