A callback function is an asynchronous equivalent for a function. It's called at the completion of each and every task. In Node.js, callbacks are generally used, and all the APIs of Node are written in a way to support callback functions.

When a function starts reading a file, it returns the control to the execution environment immediately so that the next request can be executed, and this is a perfect example of a callback function.

Example

                                        

Here’s how to write a callback function in Node.js:

var myCallback = function(data) {
  console.log('got data: '+data);
};

var usingItNow = function(callback) {
  callback('get it?');
};

Suggest An Answer

No suggestions Available!