Generator functions in JavaScript
function normalFunction() {
console.log("Executing a normal function");
console.log("Execution cannot be stopped");
}
normalFunction();
Following is the output of the above code.
The function is executed in a normal way, i.e. top to bottom and ends when the function body ends.
function* generatorFunction() {
console.log("Execution starts...");
yield 1;
console.log("Execution is midway...");
yield 2;
console.log("Execution ends...");
}
This is not a regular function, instead, it is a generator function because it is created using an asterisk (*) symbol. Moreover, inside the body, the “yield” keyword is used. Let’s understand the need for an asterisk symbol and “yield” keyword.
The asterisk symbol is used to define a generator function while the “yield” keywords pause the execution. So in simple terms, a function defined with an asterisk symbol is a generator function and whenever a yield statement is encountered in it, the execution pauses.
A generator function is not called like a regular function. To call it, first, we have to create a generator object. Observe the following code.
function* generatorFunction() {
console.log("Execution starts...");
yield 1;
console.log("Execution is midway...");
yield 2;
console.log("Execution ends...");
}
const generator = generatorFunction()
console.log(generator)
“generatorFunction()” is stored in a variable. Let’s see what is the value of “generator”.
The value of “generator” is a Generator object, meaning, the generator function returns an object.
Now, this object would be used to call the generator function. To call it, we need the in-built “next” method.
function* generatorFunction() {
console.log("Execution starts...");
yield 1;
console.log("Execution is midway...");
yield 2;
console.log("Execution ends...");
}
const generator = generatorFunction()
generator.next()
Observe the output.
If you observe carefully, only that console statement is executed which is placed before the first yield. But why? This happens because the execution stops when the yield statement is executed.
function* generatorFunction() {
console.log("Execution starts...");
yield 1;
console.log("Execution is midway...");
yield 2;
console.log("Execution ends...");
}
const generator = generatorFunction()
const next1 = generator.next()
console.log(next1)
Observe the output.
The returning object has two fields - “done” and “value”. The value of “value” is 1, which is also the value written alongside the first “yield”.
yield 1;
So, the “value” denotes the value of the “yield” while “done” whose value is “false”, denotes that function has not been executed completely.
Wrapping it up
That's all about what is generators in JavaScript and how to use them. The Generator function is a powerful feature introduced in ES6. Such types of functions are used heavily in modern JavaScript development. One of the major uses of the generator function is to handle asynchronous calls. There are several other uses.- My favorite free Courses to learn Angular and React
- The React Developer RoadMap
- 30 JavaScript interview Questions for 1 to 2 Years Experienced
- 5 Free Docker Courses for Java and DevOps Engineer
- 5 Best courses to learn React Native in depth
- The 2023 Frontend and Backend Developer RoadMap
- 5 Courses to Learn Ruby and Rails for Free
- 10 Books and Courses to learn Angular
- Top 5 Websites to learn React.js for Beginners
- My favorite books to learn React.js in depth
- 5 Free Courses to learn Kubernetes in depth
- 5 Online training courses to learn Angular for Free
- 3 Books and Courses to Learn RESTful Web Services in Java
No comments :
Post a Comment