Exploring Callbacks In Nodejs


Exploring Callbacks In Nodejs

By bangarsanju12

Callbacks basically serve the purpose of asynchronous equivalence of methods. Callbacks are used extensively in Node.js. In simple terms it is a method being called on the  completion of a task. Most of the programmable interfaces of Node.js have been designed to uphold callbacks and its extended functionalities.

For instance, a method to peruse a file/doc may start reading file/doc and once the reading execution terminates it instantly returns the control to the execution environment so as to trigger the execution lined up next. Once file I/O is complete, it will trigger the callback method, while passing the callback method, the content of the file as a param. So as to complete a seamless and completely asynchronous file I/O, and the control need not wait at any particular instant. This boosts up the scalability of Node.js and also make it an apt choice for such processing situations, as it is capable of processing a very high number of operations simultaneously without the need of waiting for any particular method to return.

Callbacks In Nodejs

Code Level Understanding Callbacks In Node

Blocking Code :-

Here, basically the control is kept blocked until the whole file/doc is read, and once the process of reading is complete then only the control moves forward. It is very clearly visible from the example below, the file’s contents are displayed first and then the control gets released to move to the next line and then the logging statement runs.

var fs = require("fs");
var data = fs.readFileSync('understandCallbacks.txt');

console.log("I hope you got the point! :)");

Non Blocking Code :-

Here, basically the control is not kept blocked until the whole file/doc is read, i.e. the program continues its execution and side by side the original task (here, the reading of file) continues and this can also be seen in the example below the console.log which is at the next line executes first and the contents of the file are displayed later.

var fs = require("fs");

fs.readFile('understandCallbacks.txt', function (err, data) {
   if (err) return console.error(err);

console.log("I hope you got the point! :)");

What is Callback Hell in Node JS

From the above explanations and depictions, it is quite evident that callbacks are a great way of handling the asynchronous nature of the language and is a very essential feature for a lot of use-cases. But along with it’s great features and functionalities, many times a very strange situation arises in front of programmers ( especially beginner programmers) which is known as callback hell.

Basically, the asynchronous, callback intensive code which usually is written is JavaScript is a bit tricky to get perfect intuitively. And the majority of the time, a lot of code looks like a pyramid.

callback hell in nodejs

From the above code sample, it can be seen clearly that the pyramid-like structure being formed is the basis of callback hell. Mainly whenever developers try to comprehend js as a code which is flowing from top to bottom, such cases occur. Majority of the time in-experienced coders end up with callback hells.

Leave a Comment