Node.js Callbacks Concept - node js - node js tutorial




Related nodejs article tags - node js - node js tutorial - node js examples

What is Callback in Node.js?

  • 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.
  • learn nodejs - nodejs tutorial - nodejs for dotnet - nodejs example programs
    Related nodejs article tags - node js - node js tutorial - node js examples

    Three major advantages of Nodejs model

    • The main event loop uses a single thread to handle multiple concurrent connections, which makes the overhead of Node.JS grow relatively slowly as the number of requests it has to serve increases as there’s no OS thread / process initialization overhead;
    • All long-running tasks (network I/O, data access, etc…) are always executed asynchronously on top of worker threads which return the results via callback to the event loop thread; and
    • JavaScript’s language features (functions as objects, closures, etc…) and Node’s programming model make this type of asynchronous / concurrent programming much easier to utilize – there’s no thread management, no synchronization mechanisms, and no message-passing nonsense. This eliminates a lot of pitfalls that most developers fall into when attempting to develop concurrent applications.

    Simple scenario - nodejs example :

  • When a function start reading file, it returns the control to execution environment immediately so that the next instruction can be executed.
  • In Node.js, once file I/O is complete, it will call the callback function. 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

 process of node js callback concept

Learn Node js - node js Tutorial - process of node js callback concept - - node - Node js Examples

  • Create a text file named input.txt with the following content
Wikitechy is giving self learning content
to learn different technologies in easy and effective manner!!!!!
  • Create a js file named main.js with the following code
Related nodejs article tags - node js - node js tutorial - node js examples

main.js

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
Related nodejs article tags - node js - node js tutorial - node js examples

Output:

Wikitechy is giving self learning content
to teach the world in simple and easy way!!!!!
Program Ended		

Non-Blocking Code Example

  • Create a text file named input.txt with the following content.
Wikitechy is giving self learning content
to learn different technologies in easy and effective manner!!!!!
  • Update main.js to have the following code
Related nodejs article tags - node js - node js tutorial - node js examples

main.js

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
Related nodejs article tags - node js - node js tutorial - node js examples

Output:

Program Ended
Wikitechy is giving self learning content
to learn different technologies in easy and effective manner!!!!!

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 "Program Ended" and at the same time, the program without blocking continues reading the file.
  • 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.

This wikitechy technological portal provides you whole lot of information related to the topics such as mongodb tutorial , what is node js , express js tutorial , node js tutorial pdf , learn node js , node js tutorial for beginners , node js tutorial w3schools , node js express , javascript for beginners , node js express tutorial , node js tutorial point , node js book , node js server , note js , node js mongodb , what is node js used for , why node js , node tutorial , node js basics , tutorialspoint node js , node express , node js sample application , nodeschool , node js for beginners , express tutorial , node js application , use of node js , npm tutorial , node js org , node js training , node js online training , node js tutorial beginner , how to use node js , node js hello world , express node js , node js mongodb tutorial , nodejs http , why use node js

Related Searches to Node.js Callbacks Concept

Adblocker detected! Please consider reading this notice.

We've detected that you are using AdBlock Plus or some other adblocking software which is preventing the page from fully loading.

We don't have any banner, Flash, animation, obnoxious sound, or popup ad. We do not implement these annoying types of ads!

We need money to operate the site, and almost all of it comes from our online advertising.

Please add wikitechy.com to your ad blocking whitelist or disable your adblocking software.

×