At Agira, Technology Simplified, Innovation Delivered, and Empowering Business is what we are passionate about. We always strive to build solutions that boost your productivity.

Generators In JavaScript With Examples

  • By Logakrishnan
  • March 30, 2020
  • 901 Views

Introduction to generators

Generators in Javascript are one of the special class functions that simplify your task of writing iterators. Javascript Generators generate a series of values. Generators can be defined as the normal functions, but they can return (yield) multiple values whereas normal functions can have only one return value.
Yield statement in the generator function will pause the function execution when it is called and it does have the functionality to resume, which will continue its execution from the last yield run.

//sample generator function definition
function* generator() {
yield 1;
yield 2;
yield 3;
…
...
}

From the above example, it looks like a normal function definition, but since it is a generator function, it has function* in it, instead of the function keyword. And it uses yield statement instead of the return statement, by which it can yield multiple values from a single function.

ALSO READ: MongoDB In Golang With Examples – A Beginner’s Guide

Generator Object

Generator functions will return objects when it is called. It can be used by looping the object because it is iterable or we can use the next method in these objects.

let genObj = generator.next();
console.log(genObj);

Output:

{ value: 1, done: false }

When the done becomes true, it means that the generator function is in its last execution, it will no more return values.
So we can get the value by using the generator object,

let genObj = generator();
console.log(genObj.next().value);
console.log(genObj.next().value);
console.log(genObj.next().value);
console.log(genObj.next().value);
Output:
1
2
3
undefined

When the last yield statement returns it’s valued, if it is called again, then it will return the value undefined.
Once the generated value is exhausted, we cannot iterate it again. All we can do is to create a new generator object & then use it.

Iterating Generator

Generators are also iterables, hence we can simplify the code for the implementation of the iterator. And we need not to manually store the next() method and the current state.

function* generator() {
yield ‘hello’;
yield ‘world’;
yield ‘program’;
}
for(let item of generator) {
console.log(item);
}

Output:

hello
world
program

ALSO READ: How To Develop A Healthcare Mobile Application For Hospitals

Infinite Data Generator Functions

It is possible to create generator functions that will never end. In the below function, the while loop will always become true, and it will yield value whenever the function is called.

function* infinite() {
let num = 100;
while (num) {
yield num;
num = num + 1
}
}
let numbers = infinite();
console.log(numbers.next().value);
console.log(numbers.next().value);
Output:
100
101

Generators Observers

Generators will act as an observer since it will act when it receives a value. It will receive value from the next() function. Hence it keeps observations for value when it receives it will act immediately.

Example:
genObj.next(200).value

Do you find it interesting? you might also like these articles. Top 10 Best Tech Companies For Employees To Work In The USA In 2020 and Top 10 IT Staffing and Recruiting Agencies in the USA.
If you have a business idea in your mind and in search for a reliable web development company, you are in the right place. Hire the best web developers in the industry from Agira technologies.

Looking for a Tech partner to dominate the digital world?

Logakrishnan

A Full-Stack web developer, expertise in various Javascript frameworks including Node JS and React JS. He is fond of exploring ethical hacking and penetration testing. Logakrishnan is a person who loves to share his knowledge of technologies and is also an active instructor at Udemy.