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);
   console.log(data.toString());
});

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){
	if(err){
		console.log(err)
	}else{
		console.log(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

Myself KarthiQ, I am the author of this blog, I know ways to write a good article but some how I donot have the skills to make it to reach people, would you like help me to reach more people By sharing this Article in the social media.

Share this Article Facebook
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions

Recent Addition

new tutorial Selenium Online Training : Our next online training course for Selenium with Java starts from 17th December 2018.

You can attend first 3 classes for free, the total course fee is INR 10,000

The course time would be 8.00 PM(IST) for the first three classes

If you are interested to learn, then you can join the course by sending email to chercher.tech@gmail.com

or Register below


 
Join My Facebook Group
Join Group