1.

How do you write a callback function in node JS?

Answer»

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?');
};



Discussion

No Comment Found