DEV Community

Cover image for What are Generators in JavaScript?
Rahul
Rahul

Posted on

What are Generators in JavaScript?

In today's post, we'll cover Generator function in JavaScript. This post is important for people who have some knowledge about JavaScript and want to upgrade that.


Generators

Generators, are a function which can be stopped in mid-way and continued from where it is stopped. In brief, generators are appeared to functions but behaves like iterators.

// Generator function Syntax
function* name() {
    yield statement; 
}
Enter fullscreen mode Exit fullscreen mode

Keyword 'Yield'

Generator function can have a keyword yield. It is an operator with which generator can pause itself. Whenever the generator encounters the keyword yield it returns the value specified after it.

Generator.prototype.next()

Generator function has a next() method. Every invocation of next() yields an object. This method returns an object with two properties, done and value.

When a generator function has nothing left to yield, the done property is set to true and value will be undefined.

INFINITE DATA STREAMS

It is possible to create generators that never ends.

function * generatorFunction() {
    var num = 1; 
    while(num > 0) {
        yield num; 
        num = num + 1; 
    }
}
var number = generatorFunction(); 
console.log(numbers.next()); 
// {"value":1,"done":false}
console.log(numbers.next());
// {"value":1,"done":false}
Enter fullscreen mode Exit fullscreen mode

Generator.prototype.return()

Generators function has a return() method. This method returns the argument passed as value and finishes the generator. If next() is invoked after return() the value will be undefined.

If argument is passed to return(), the value property of the returned object will be the argument and done property will be true.

function* generatorFunction() {
    yield 'I2i'; 
    yield 'Rahul'; 
}
var genFunc = generatorFunction();
console.log(genFunc.next()); 
// {"value":"I2i","done":false}
console.log(genFunc.next(1)); 
// {"value":"I2i","done":true}
console.log(genFunc.next()); 
// {"done":true}

Enter fullscreen mode Exit fullscreen mode

Using return keyword

Mostly we use yield keyword to return an object in the generator function. But we can also use return keyword. The return keyword sets the one property of the returned object as true after which generators cannot generate any more values.

function* generatorFunction() {
    yield 'I2i'; 
    return 'Completed'; 
    yield 'Rahul'; 
}
var genFunc = generatorFunction();
console.log(genFunc.next()); 
// {"value":"I2i","done":false}
console.log(genFunc.next()); 
// {"value":"I2i","done":false}
console.log(genFunc.next()); 
// {"done":true}

Enter fullscreen mode Exit fullscreen mode

Generator.prototype.throw()

Generator function has throw() method. This method throws an exception and returns an object with two properties, done and value.

function * generatorFunction() {
    var num = 1; 
    while(num > 0) {
        try { 
          yield num; 
          num = num + 1;
        } catch(error) {
            console.log("Error Occurred"); 
        }
    }
}
var number = generatorFunction(); 
console.log(numbers.next()); 
// {"value":1,"done":false}
console.log(numbers.trow(new Error()));
//Error Occurred
// {"value":2,"done":false}
Enter fullscreen mode Exit fullscreen mode

Using throw keyword

When we use throw keyword, the iterator done property is set to true and Error is thrown and the program stopped.

function* generatorFunction() {
    yield 'I2i'; 
    throw ErrorMessage; 
    yield 'Rahul'; 
}
var genFunc = generatorFunction(); 
console.log(genFunc.nect()); 
// {"value":"I2i","done":false}
console.log(genFunc.nect()); 
//Uncaught ErrorMessage
Enter fullscreen mode Exit fullscreen mode

😎Thanks For Reading | Happy Coding⚡

Top comments (0)