A callback works same as a function but it operates asynchronously. The callback function is called immediately after the completion of each task. Callbacks are generally used in Node.js and also in many programming languages.

Topics Covered

  • Callback
  • How does a callback work?
  • Blocking call
  • Non-blocking call

Callback

In Node.js-Callback Concept, all the APIs of Node are coded in a way to support callbacks. Once file Input/Output is complete, it calls the callback function. So there is no blocking or wait for any file I/O. The Node.js is highly scalable because it can process the maximum number of a request without waiting for any other functions to the return the result.

How does a callback work?

The callback is executed inside the containing function’s body, whenever a callback function is passed as a parameter to another function. Since this function has the callback function in its parameter (as a function definition), it can execute the callback anytime.

The callback function does not execute immediately. At some specified point, It is “called back” inside the containing function’s body.

The callback functions are of two types:

  1. Blocking calls
  2. Non-Blocking calls

Blocking Calls

Blocking code Example :

Create a text file named index.txt with the following content −

Hello User!!!
Welcome to Pabbly.com!!!!!

Create a js file named demo.js with the following code −

var fs = require("fs");

fs.readFile('index.txt', function (err, data) {
   if (err) return console.error(err);
   console.log(data.toString());
});

console.log("Program Ended");

Now run the demo.js to view the result −

$ node demo.js

Verify the Output:

Hello User!!!
Welcome to Pabbly.com!!!!!
Program Ended

Non-Blocking Calls

Non-Blocking code Example :

Create a text file named index.txt with the following content-

Hello User!!!
Welcome to Pabbly.com!!!!!

Update demo.js to have the following code −

var fs = require("fs");

fs.readFile('index.txt', function (err, data) {
   if (err) return console.error(err);
   console.log(data.toString());
});

console.log("Program Ended");

Now run the demo.js to see the result −

$ node demo.js

Verify the Output.

Program Ended
Hello User!!!
Welcome to Pabbly.com!!!!!

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

  • The Blocking-code example shows that the function will first read the file and then only it will proceed to the end of execution.
  • The Non-blocking code example shows that the program does not wait to read the file and proceed to print “Program Ended”. At the same time without blocking the program, it continues reading the file.

After comparing both the callback concepts, we can conclude that a blocking code executes in a sequential manner whereas non-blocking code does not execute sequentially. If a program needs the data to be processed, it should be kept within the same block to execute it sequentially.


Conclusion 

In this blog, You covered the callback concept in Node.js which is required by a beginner developer to the work. Under this callback concept function, we have demonstrated two examples, one for the blocking call and other for the non-blocking call. After you can see the comparison between the Blocking and Non-blocking concepts. So we can conclude that a blocking program executes in a sequential manner whereas a non-blocking program does not execute sequentially. 

Learn More-