Node.js Callbacks
Node.js Callbacks
When we say about the Node.js callback, it is an asynchronous equivalent for a function. And it is called whenever any of task is completed. And whne we say about the Node.js, here in Node.js callbacks are generally used. And all of the APIs of Node.js are mainly written in such a way that it will supports callbacks. To understand this we will take an example, let suppose when a function start reading of any file, it will return the control to execution environment immediately so that the next instrution can be executed.
When file I/O file operation is complete, it will call the callback function. SO that there is no blocking or wait for File I/O. And this feature will makes Node.js highly scalable, As due to this feature it can process high number of request without waiting for any function to return result.
Blocking Code Example
To understand this you need to follow these steps:-
(1)In first step create a text file which named as input.txt which is having below content:-
Crackyourinterview is online platform which will provide self learning tutorials on many technologies. And try to covers all topics.
(2)Now in this step create a JavaScript file which name as main.js which is having below code:-
var fs = require("fs");
var data = fs.readFileSync('input.txt');
console.log(data.toString());
console.log("Program Ended");
(3)Now in this step we open Node.js command prompt and execute the below code
node main.js
Once you run above command you will get the output as given below.
Non Blocking Code Example
To understand this you need to follow these steps:-
(1)In first step we will create a text file which name is input.txt which is having the below content:-
Crackyourinterview is online platform which will provide self learning tutorials on many technologies. And try to covers all topics.
(2)Now in this step we will create a JavaScript file which name is main.js which have the below code:-
var fs = require("fs");
fs.readFile('input.txt', function (err, data)
{
if (err) return console.error(err);
console.log(data.toString());
});
console.log("Program Ended");
(3)Now in this step we will open Node.js command prompt and execute the below code to run the file:-
node main.js
Once you run above command you will get the output as given below.
As per the above two example, you will get the details of blocking and non-blocking calls. Here in above first example it shows that programs blocks until it reads the file and then only it proceeds to end the program and on other hand in second example, program will not wait for file reading but it just proceeded to print "Program Ended" and same time program without blocking continues reading the file.
So we can say that, a blocking program executes very much in sequence. And we can easily implement the logic from programming point of view in block programs. But non-blocking programs does not execute in sequence, so in case a program needs to use any data to be processed, it should be kept with-in the same block to make it sequential execution. | |