Node.js: Callbacks - Day Four

Datetime:2016-08-23 05:17:34          Topic: Node.js           Share

Synchronous (Blocking)

Blocking or Synchronous means your code can’t be run because some other process prevents it. Your process will wait for the completion of another process and your process will start to execute after the completion of the process, which is blocking your process. Let's take an example.

I have a text file, which contains some data.

Now, I will try to read this file.

Code

var fs = require("fs");  
var data = fs.readFileSync('E:/data.txt');  
console.log(data.toString());  
Callback();  
  
function Callback() {  
    console.log("***************");  
    console.log("This is Callback function");  
}  

Output

This is an example of blocking (Synchronous ) process because in this process, we are reading the data from a text file. System will execute this task. First, it doesn’t matter, how much long time is taken by this task? After the completion of this task, the next line of code will execute. Hence, the text of “data.txt” file will read and Callback function will be called.

Asynchronous (Non-Blocking)

Asynchronous or Non-Blocking process means if any process takes long time, CPU doesn’t wait for the completion of the current process. Instead of this, CPU will start the execution of the next steps and the current process is also run in the background. Let’s take an example.

Example

var fs = require("fs");  
var data = fs.readFile('E:/data.txt', function(error, data) {  
    console.log(data.toString());  
});  
Callback();  
  
function Callback() {  
    console.log("***************");  
    console.log("This is Callback function");  
}  

Output

What is a Callback ?

Callback function is a function, which is passed to another function, which will be invoked at some point, fenerally, after the completion of any task. The idea behind the callback function is an asynchronous programming implementation. Suppose, we are reading a large file and we don’t want to keep Node.js Server busy during this period. Thus, we tell to perform this operation in the background and call a function, when it completes. This function is called as a callback function.

Example

var fs = require("fs");  
console.log("Fs Module implemented");  
var CRS = fs.createReadStream('E:/data.txt');  
console.log("Callback function for data");  
CRS.on('data', writeCallback);  
console.log("Callback function for Close");  
CRS.on('close', closeCallback);  
  
function writeCallback(data) {  
    console.log(data.toString());  
}  
  
function closeCallback() {  
    console.log("File read Successfully");  
}  
console.log("!!Finish");  

Output

The code given above, shows a perfect example of Non-blocking and callback concept. In this example, we are reading a file from the system and implement two Call back functions, namely “writeCallback” and “closeCallback” function, which will be call “data” read and “close” events.

How Node.js perform Non-Blocking and Asynchronous Task

Node.js contains single thread. Afterwards, it maintains the non-blocking and asynchronous mechanism. Answer of this question is that Node.js uses the observer pattern. It means Node.js performs an event loop, using “libuv” library. Whenever any task completes, it runs the corresponding event, using the callback function.

Node.js contains Chrome’s V8 engine, which is used to run JavaScript code, which runs single set at a time or single line at one time.

Node.js also contains another library, which is “libuv”. This library is responsible to perform the Node.js task at the lower level at an operating system level like read the files, connection with the database or download something from the Server. “Libuv” library contains a queue of an event, which has been completed. Major part of “Libuv” is an Event Loop. It is a loop, which checks for the completion of any task and if any vent completes, it invokes the event’s corresponding callback function. The image, given below, describes the whole process:

“Libuv” library performs the task at the lower level(OS level). When OS completes any task, it writes “task done” status in queue. “Libuv” checks the queue constantly, using the Event Loop. If it finds the completion of any task, it invokes the corresponding callback function. In node.js, each inbuilt module performs the asynchronous task. Each asynchronous function accepts a callback function, which will invoke on the completion of the task. I think, now it is clear, how Node.js application works?

I hope, you liked this article. If you have any doubt, write in the comment section.





About List