We now have a youtube channel. Subscribe!

Node.js | Callbacks Concept

Node.js | Callbacks Concept


Hello folks! welcome back to a new edition of our tutorial on Node.js. In this section of our tutorial on Node.js, we will be studying about Node.js Callbacks Concept.

Callback is an asynchronous equivalent for a function. A callback function is called at the end of a given task. Node makes heavy use of callbacks. All the APIs of node.js are written in such a way that they have support for callbacks.

For example, a function that is to read a file may start reading file and return the control to the execution environment immediately so that the next instruction can be executed. As soon as file I/O is finished, it will call the callback function while passing the callback function, the file content as a parameter. So there is no blocking or wait for File I/O. This makes the Node.js highly scalable, as it can process a high number of requests without waiting for any function to return results.


Blocking Code Example

Create a text file input_txt with the following content -

WebDesignTutorialz is offering self learning content
to teach the world in simple and easy manner!!!!!

Create a js file, main.js with the below code -

var fs = require("fs");
var data = fs.readFileSync('input.txt');

console.log(data.toString());
console.log("Program Ended");

Now, run the main.js to see the result -

$ node main.js

Verify the result.

WebDesignTutorialz is offering self learning content
to teach the world in simple and easy manner!!!!!
Program Ended


Non-Blocking Code Example

Create a text file input_txt with the following content -

WebDesignTutorialz is offering self learning content
to teach the world in simple and easy manner!!!!!

Update main.js to have the following 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");

Now, run the main.js to see the result -

$ node main.js

Verify the result.

Program Ended
WebDesignTutorialz is offering self learning content
to teach the world in simple and easy manner!!!!!

These two examples explain the concept of blocking and non-blocking calls.

  • First example shows that the program blocks waits until it reads the file and then it proceeds to end the program.
  • The second example shows that the program doesn't wait for file reading and moves to print "Program Ended" and at the same time, the program without blocking continues reading the file.

Therefore, a blocking program execute very much in sequence. From the programming point of view, it's easier to execute the logic but non-blocking programs don't execute in sequence. In case a program needs to use any data to be processed, it should be kept within the same block to make it sequential execution.


Alright guys! This is where we are going to be rounding up for this tutorial. In our next tutorial, we will be studying about Node.js Event Loop.

Feel free to ask your questions where necessary and we will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.

Follow us on our various social media platforms to stay updated with our latest tutorials. You can also subscribe to our newsletter in order to get our tutorials delivered directly to your emails.

Thanks for reading and bye for now.

Post a Comment

Hello dear readers! Please kindly try your best to make sure your comments comply with our comment policy guidelines. You can visit our comment policy page to view these guidelines which are clearly stated. Thank you.
© 2022 ‧ WebDesignTutorialz. All rights reserved. Developed by Jago Desain