Node.js - Callbacks

Node JS handle all asynchronous calls via callback. Hey wait, what is callback..??
well, callbacks in the general definition are just functions that you pass to other functions. It is called at the completion of certain task.

In Node.js, callbacks are generally used. All APIs of Node are written in a way to supports callbacks.

A callback function, also known as a higher-order function, is a function that is passed to another function (let’s call this other function "otherFunction") as a parameter, and the callback function is called (or executed) inside the otherFunction.

Doesn’t make any sense..!! function passing to another function…!! what’s going on here..?? let me give a simple example of callback

var fs = require("fs");

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

fs module is built-in module of Node JS which is used to work with file system of your computer. Here i’m using fs.readFile() function to get file content of input.txt, which give information about contents of the file

Let’s analyze how this code snippet is working. Here we can see that fs.readFile()function is taking two arguments. First argument is the path of the file and second argument is a function, which is nothing but a callback function.

Let’s get into more details of code. First fs.readFile() will try to find whether the file exists or not, then it will try to get all information about that file. If it successfully get all information then that data is passed into data parameter of callback function.

If some error happen in the process, that error is passed into err parameter of callback function. After dealing with the path, callback function get invoked.

Note : According to coding convention of Node JS, the first argument in callback function is for an error object and second argument is for a successful response

If you are making your own custom callback function, it is advisable to follow the same rules.

const checkVariableType = function(arg, callback){
	if(typeof arg !== number){
		return callback("not a number type");

checkVariableType(15, function(err, data){

Here we define a function with two parameters

  • arg: any argument you pass
  • Callback : a callback function

Assign that function to a constant variable checkVariableType, Now we are calling the function from line 10 with the help of checkVariableType which takes two parameters 15 which is a number and A callback function

Try to pass a string as parameter in the place of 15 and see the output.

About Author

Article is written by Pavan (a) KarthiQ. Well, I am serving notice period in an MNC, Bangalore. I thought to enrich every person knowledge a little, I always have a feeling, when we teach something, we will learn more than what you know. Knowledge is the only thing that doubles when you spend it.

I have also created the reporter for Protractor Jasmine. Use for your projects without any hesitation

Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions