async - await In JavaScript In Hindi | JS async - await In Hindi
Important JavaScript Interview Questions In Hindi
Prmise.all() Vs Promise.allSettled() | JS allSettled() Example
Nullish coalescing assignment In JavaScript In Hindi
JavaScript Interview Question For Beginners In Hindi
Linux cat command Example | Linux print file content on terminal
What is TCP/IP Model In Hindi | Layers of TCP/IP Model
If tutorials available on this website are helpful for you, please whitelist this website in your ad blocker😭 or Donate to help us ❤️ pay for the web hosting to keep the website running.
JavaScript In Hindi by Gabriel Heinzer on Unsplash
Normal Functions के अलावा JavaScript में Generator Functions भी होते हैं , जो कही कही पर काफी useful होते हैं।
Generators वो functions होते हैं जिनका execution exit होकर दुबारा re-enter हो सकते हैं , asynchronous programming में ये काफी important tool होता है।
Generators को define करने के लिए function*
का use किया जाता है और , function के अंदर yield
का use किया जाता है , जो Generator object को return करेगा।
function* my_generator() {
yield 1;
}
console.log(my_generator());
Generator { }
इस generator के कई useful methods हैं जिन्हे हम आगे समझेंगे।
function* range(start, end) {
for (let i = start; i <= end; i++) {
yield i;
}
}
let myRange = range(1, 2)
console.log(myRange.next());
console.log(myRange.next());
console.log(myRange.next());
Object { value: 1, done: false } Object { value: 2, done: false } Object { value: undefined, done: true}
ऊपर दिए गए example में आप देख सकते हैं yield
के साथ जो value use की गयी है वही return हो रही है और , done false
आ रहा है जब तक कि कोई next value मिलेगी। जैसे ही loop end हुआ हमें done : true
मिल रहा है।
generator function को call करने पर immediately function body execute न होकर generator object return होता है। और जब iterator का next()
method call होता है तो first yield expression तक code execute होता है जो एक object return करता है जिसमे 2 properties : value , done होती हैं। जैसा कि आप example में देख भी सकते हैं।
generator function में yield के बाद return
keyword का मतलब है generator function finish हो गया है और done property true
मिलेगी , फिर आप next()
method use करोगे तो value में undefined ही मिलेगा।
हालाँकि returned value भी आपको next()
की तरह ही मिल जायगी।
function* test(start, end) {
yield "hi !";
return "bye ..";
yield "It will not work..";
}
let f = test(1, 2)
console.log(f.next());
console.log(f.next());
console.log(f.next());
Object { value: "hi !", done: false } Object { value: "bye ..", done: true } Object { value: undefined, done: true}
आप generator object के next() method में argument भी pass कर सकते हैं , ध्यान रहे हम next() method में value pass करेंगे तो yield के साथ function में handle करेंगे।
function* logGenerator() {
console.log(yield);
console.log(yield);
console.log(yield);
}
const gen = logGenerator();
gen.next();
gen.next("Hello");
gen.next("Hi");
gen.next("Bye ..");
Hello Hi Bye ..
ध्यान रहे कि generator functions को as a constructor
use नहीं कर सकते हैं मतलन इनका आप object
नहीं बना सकते हैं।
function* test(){
// code.
}
new test();
TypeError: test is not a constructor
***
I Hope, आपको javascript में generator functions के बारे में अच्छे से समझ आ गया होगा।
Loading ...
Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)
Get connected with me. :) LinkedIn Twitter Instagram Facebook