Node-js
Node.js Vs Python: What's the Difference?
What is Node.js? Node.js is a server-side platform built on Google Chrome's JavaScript Engine. It uses a...
In this tutorial, we are going to learn about Generators and their differences with Callbacks
Generators have become quite famous in Node.js in recent times and that probably because of what they are capable of doing.
Generators have the below 2 key methods
Let's look at an example of how generators can be used.
In our example, we are going to have a simple Add function which will add 2 numbers, but we will keep on halting the method execution at different points to showcase how generators can be used.
function* Add(x) {
yield x + 1;
var y = yield(null);
y = 6
return x + y;
}
var gen = Add(5);
gen.next();
gen.next();
Code Explanation:-
Generators are used to solve the problem of what is known as callback hell. Sometimes callback functions become so nested during the development of a Node.js application that it just becomes too complicated to use callback functions.
This is where generators are useful. One of the most common examples of this is when creating timer functions.
Let's see the below example of how generators can prove to be useful over callbacks.
Our example will just create a simple time delay function. We would then want to call this function incorporating a delay of 1000, 2000 and 3000 ms.
Step 1) Define our callback function with the necessary time delay code.
function Timedelay(ptime, callback) {
setTimeout(function() {
callback("Pausing for " + ptime);
}, time);
}
Code Explanation:-
Step 2) Now let's look at the code if we were incorporating callbacks. Suppose we wanted to incorporate callbacks based on the value of 1000, 2000 and 3000 milliseconds, the below code shows how we would need to implement these using callbacks.
Timedelay(1000, function(message) {
console.log(msg);
Timedelay(2000, function(message) {
console.log(msg);
Timedelay(3000, function(message) {
console.log(msg);
})
})
})
Code Explanation:-
From the above code, you can see that it becomes messier as we want to start calling the function multiple times.
Step 3) Now let's see how to implement the same code using generators. From the below code you can now see how simple it has become to implement the Timedelay function using generators.
function* Messages() {
console,log(yield(Timedelay(1000, function(){})));
console,log(yield(Timedelay(2000, function(){})));
console,log(yield(Timedelay(3000, function(){})));
}
Code Explanation:-
Summary
Generators can also be used to alleviate the problems with nested callbacks and assist in removing what is known as the callback hell. Generators are used to halt the processing of a function. This is accomplished by usage of the 'yield' method in the asynchronous function.
What is Node.js? Node.js is a server-side platform built on Google Chrome's JavaScript Engine. It uses a...
The Node.js framework is mostly used to create server-based applications. The framework can easily be...
In previous tutorials, you would have seen callback functions which are used for Asynchronous...
Introduction to Node.js The modern web application has really come a long way over the years with...
In this tutorial, we will study the Express framework. This framework is built in such a way that...
What is Node JS? Node.js is a cross-platform runtime library and environment for running...