Tuesday, May 12, 2015

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 APIs of Node are written is such a way that they supports callbacks.
For example, a function to read a file may start reading file and return the control to execution environment immidiately so that 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 parameter. So there is no blocking or wait for File I/O. This makes Node.js highly scalable, as it can process high number of request without waiting for any function to return result.


Blocking Code Example

Create a text file named input.txt having following content

                    This Blog is provide self learning technique 
                    to learn the things in simple and efficience way.
 
Create a js file named main.js which has the following 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 Output


                    This Blog is provide self learning technique 
                    to learn the things in simple and efficience way.
                    Program Ended
 

Non-Blocking Code Example

Create a text file named input.txt having following content

  
 
This Blog is provide self learning technique 
to learn the things in simple and efficience way.
 
Update main.js file to have 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 Output 
 
 
               
              Program Ended 
              This Blog is provide self learning technique 
              to learn the things in simple and efficience way. 
  
 
These two examples explain the concept of blocking and non-blocking calls. First example shows that program blocks until it reads the file and then only it proceeds to end the program where as in second example, program does not wait for file reading but it just proceeded to print "Program Ended" and same time program without blocking continues reading the file.
Thus, a blocking program executes very much in sequence and from programming point of view its easier to implement the logic but non-blocking programs does not execute in sequence, so in case a program needs to use any data to be processed, it should be kept with-in the same block to make it sequential execution.

You can start from here for More..
 
 
 




4 comments:

  1. Best article, very useful and explanation. Your post is extremely incredible. Thank you very much for the new information.
    Node Js Training Center in Hyderabad
    Node Js course with placement in Hyderabad
    Node Js course in Hyderabad with placement

    ReplyDelete
  2. http://chennaitraining.in/android-training-in-chennai/
    http://chennaitraining.in/c-c-plus-plus-training-in-chennai/
    http://chennaitraining.in/ios-training-in-chennai/
    http://chennaitraining.in/iot-training-in-chennai/
    http://chennaitraining.in/full-stack-developer-training-in-chennai/
    http://chennaitraining.in/devops-training-in-chennai/
    http://chennaitraining.in/datastage-training-in-chennai/

    ReplyDelete