Monday, November 18, 2024
Google search engine
HomeLanguagesJavascriptExplain different kinds of generators in JavaScript

Explain different kinds of generators in JavaScript

Generators are the function whose execution can be paused and resumed as per the requirement of the programmer. We use the yield keyword in this function to stop the execution of code. Generators are very useful in asynchronous programming. Generators combine with different features of programming language and make itself vary useful for programmer for use it for effective programming. 

Generators can be created using the generator function expression and Generator function constructor:

Function expression:

function* FUNCTION_NAME( argu1[, argu2[,...argu3]]]) { 
    // Generator body
}

Generator function constructor:

var generator_constructor = 
    Object.getPrototypeOf( function*(){} ).constructor
    
var FUNCTION_NAME = generator_constructor()

Because Generators are vary useful. Let’s talk about different kinds of generators in javascript: 

Normal Generator: It is the generator kind in which a generator like an iterator generates the next value after the execution of each next() method to the generator function.  Here in this kind, we will yield numbers in a continuous manner till the last yield keyword. 

Javascript




function* generator(){
    for(var i = 0 ; i<5; i++){
        yield i;
    }
}
 
var first_gen = generator();
 
console.log(first_gen.next().value);
console.log(first_gen.next().value);
console.log(first_gen.next().value);
console.log(first_gen.next().value);
console.log(first_gen.next().value);


Output:

0
1
2
3
4

Generator with Parameters: In this Generator, we pass the parameter to the generator’s next method and it replaces this parameter with the last yield keyword which is before pausing the execution of the function. Here the first next method of generators function’s parameter will not replace with any yield keyword because no yield keywords are before the first pause of function. In the same manner seconds, call of next method with parameter is replaced with the first yield keyword and the third next method parameter is replaced with second yield keyword.

Javascript




function* generator(){
    para1 = yield ;
    console.log(para1);
    para2 = yield ;
    console.log(para2)  
}
 
var first_gen = generator();
first_gen.next("this is not going to assign any one");
first_gen.next("I am first");
first_gen.next("I am second")


Output:

I am first
I am second

Generator with Object property: In this kind of Generator, Generator is defined as the property of the Object. We can create a generator instance and then execute the next method for further execution of the generator. In this case, the generator is as same as a normal generator but it is a property of an Object. 

Javascript




var temp = {
    *generator(){
        yield "1 yield";
        yield "2 yield";
    }
}
 
var gen = temp.generator();
 
console.log(gen.next().value);
console.log(gen.next().value);


Output:

1 yield
2 yield

Generator with Another Generator: In this kind of Generator we have a generator that contains a Yield with another Generator inside its function body. When execution encounters yield with another generator it finishes execution of that generator and then continues with the primary generator. 

Javascript




function* generator1(){
    yield "this is first Generator";
}
 
function* generator2(){
    yield* generator1();
    yield "this is second Generator";
}
 
var gen = generator2();
 
console.log(gen.next().value);
console.log(gen.next().value);


Output:

this is first Generator
this is second Generator

Generator with Return Keyword: In this kind of generator, the function Body contains a return Keyword. Here when the generator function encounters the return keyword it stops and exits from the execution of the function and returns the value generator object. After the return keyword will never be executed in the execution of our program. 

Javascript




function* Return_generator(){
    yield "First";
    return "Second";
    yield "Infinite";
}
 
var gen = Return_generator();
 
console.log(gen.next().value);
console.log(gen.next().value);


Output:

First
Second

Generator created as Object method: We can create Generator as a method of Object and can use them as per requirement. In this kind, as we create a normal function to Object same we create Generator function of Object. We can use the Generator function with the instance of Object. In this case, Generator is available as the method of Object. 

Javascript




class temp{
*generator(){
    yield "Generator with object method ";
    yield "Second flow of Generator";
}
}
var temp2 = new temp()
var gen = temp2.generator();
 
console.log(gen.next().value);
console.log(gen.next().value);


Output:

Generator with object method 
Second flow of Generator

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

RELATED ARTICLES

Most Popular

Recent Comments