This tutorial is all about Generators in NodeJS and their differences with Callback. So without further delay let’s dive right in.
What are generators?
In recent years, Generators are getting more and more popular day by day in Node.js. This popularity is because of what Generators are capable of doing.
- These are the function executions that can be paused and played later.
- The concept of “Lazy execution” can be implemented using Generators. Suspending and resuming execution at your own will is the concept of “lazy execution”.
The 2 key values of Generators are:
- Yield method – This method is for halting the execution of a function at any line you want. We call Yield method where we want to halt the execution.
- Next method – We call this method to resume the execution. We call Next method in the main application. The execution continues till the next Yield or the end of the method.
Now we will be looking at some examples where we will use Generators in NodeJS.
For our first example, we have created an add method that will just add two numbers. In this method, we will halt the add method multiple times at different points to see how generators work.
function* Add(a) {
yield a + 1;
var b = yield(null);
b = 6
return a + b;
}
var gen = Add(5);
console.log(gen.next().value);
console.log(gen.next().value);
console.log(gen.next().value);
Code Explanation:-
- In the first line, we defined the generator function. We define generator function by adding a ‘*’ after the function keyword. We defined Add function with a parameter a.
- Only generators can use Yield keywords. Yield halts the function in the second line till we invoke the next() function.
- var gen = Add(5); is where we call the generator function and we send the value of 5 initially.
- gen.next().value will make the Add() function resume execution.
Callbacks vs. generators
The callback hell problem can be solved using Generators. Sometimes callback functions are so nested that it gets complicated to use callback functions. Generators come into action in such situations. They prove to be very useful in such situations. Let’s look at another example of timer functions. We will create simple time delay functions. We will call the function with the delays of 1000, 2000, and 3000 ms in this example.
Step 1) Define our callback function with the necessary time delay code.
function Delay(time, callback) {
setTimeout(function() {
callback("Pausing for " + time);
}, time);
}
Code Explanation:-
- We created function called Delay with a parameter “time”. Time is for taking in the time delay.
- This piece of code displays the time delay of the application in the form of a message.
Step 2) Now let’s see if we incorporate callbacks. Let’s incorporate callbacks based on values of 1000, 2000, and 3000 milliseconds. We will implement these delays using callbacks in the code below.
Delay(1000, function(message) {
console.log(message);
Delay(2000, function(message) {
console.log(message);
Delay(3000, function(message) {
console.log(message);
})
})
})
Code Explanation:-
- We are calling the Delay as callback with 1000, 2000, and 3000 as the values respectively
Output:-
Step 3) In this step we will use an alternative way to implement the same code. We will be using generators for this purpose.
function* Messages() {
console,log(yield(Timedelay(1000, function(){})));
console,log(yield(Timedelay(2000, function(){})));
console,log(yield(Timedelay(3000, function(){})));
}
Code Explanation:-
- In the first line we have defined the Generator function
- In the code yield(Timedelay(1000, function(){})) Yield function along with Delay function is called with 1000, 2000 and 3000 as parameters one by one.
Output:-
So, this is it for today’s tutorial. We learned about Generators in NodeJS by looking at different examples. For more tutorials like these, you can visit https://nodejstutor.com/ or you can check out the documentation of NodeJS by visiting https://nodejs.org/en/ for unlimited learning.