Node.js – Callbacks Concept

Node.js – Callbacks Concept

What is Callback?

Callback is an asynchronous equivalent for a function. A callback function is called at the completion of a given task. Node makes heavy use of callbacks. All the APIs of Node are written in such a way that they support callbacks.
For example, a function 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. Once file I/O is complete, it will call the callback function while passing the callback function, the content of the file as a parameter. So there is no blocking or wait for File I/O. This makes 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 named test.txt with the raw content −

this file has some raw content
for demonstration purpose
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat."

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

var fs = require("fs");
read_file = fs.readFileSync('test.txt');
console.log(read_file.toString());
console.log("process finished!");

Now run the app.js to see the result −

$ node app.js

Verify the Output.

this file has some raw content
for demonstration purpose
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat."
process finished!

Non-Blocking Code Example

Create a text file named test.txt with the following content.

this file has some raw content
for demonstration purpose
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat."

Update app.js to have the following code −

var fs = require("fs");
fs.readFile('test.txt',function(err, data){
if (err) return console.error(err);
console.log(data.toString());
});
console.log("process finished!");

Now run the app.js to see the result −

$ node app.js

Verify the Output.

process finished!
this file has some raw content
for demonstration purpose
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat."

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

  • The first example shows that the program blocks until it reads the file and then only it proceeds to end the program.
  • The second example shows that the program does not wait for file reading and proceeds to print "process finished!" and at the same time, the program without blocking continues reading the file.
    Thus, a blocking program executes very much in sequence. From the programming point of view, it is easier to implement the logic but non-blocking programs do not 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.
Node.js – NPM (Prev Lesson)
(Next Lesson) Node.js – Event Loop