Node.JS Callback Mechanism

In one of the earlier articles, where we studied the features of Node.js, we said that node.js follows event-driven and non-blocking model thanks to node.js callback mechanism. However, we did not delve into callbacks in detail. In this article we will dig deeper into callbacks. We shall study what callbacks are and how they help in implementing non-blocking code.

Callbacks are basically asynchronous functions that are called whenever a certain task is completed. Almost of all of the node.js modules and libraries have call back functions.

Let’s explain callbacks with the help of an example. Suppose, you have some piece of code which reads content of the text file. After the code you print a statement on the command window. In case of blocking code, your program will first read all the contents of the fall and then print the statement on the screen. This is blocking code, your code blocks and doesnt further execute unless all the contents of the file have been read. On the other hand, in case of non-blocking code, once the call to read the content of the file is made, the callback function is also specified and the next line of the code is executed without blocking the code for complete reading of file content. Finally, when he contents of the file have been read, the corresponding callback function executes.

Blocking Code Example

Create a text file named “myfile.txt” and add the following text in the file.

Knowledge hills is a website that contains free of cost programming tutorials

Learn and Build apps for free.

Now create a file named “blocking.js” in the same directory as “myfile.txt” and add the following line of code in it.

var filestream = require("fs");

var content = filestream.readFileSync('myfile.txt');

console.log(content.toString());
console.log("Code after file read");

Download the code

In the above code we have used “readFileSync” method of the file stream module “fs”. This method is synchronous and reads the content of the file in blocking mode. Now run the above file using “node blocking.js”. In the output you shall see that first the content of the file are printed on the screen and then the message after the “readFileSync” function is printed on the screen.

Non-blocking Code Example

Now let’s see how we can read the contents of the file in non blocking manner. Again create a file named “nonblocking,js”. The contents of the file are as follows.

var filestream = require("fs");

filestream.readFile('myfile.txt', function (error, content) {
   if (error) return console.error(error);
   console.log(content.toString());
});

console.log("Code after file read");

Download the code

In the above case, you shall see that the call for reading file will be sent and then immediately after that the message below the call shall be printed. Once the file reading completes the callback function prints the contents of the file. This is how node.js callback mechanism works.

Copyright 2005-2016 KnowledgeHills. Privacy Policy. Contact .