What is callback?

callback is function which run one function after another. Node is single-threaded and asynchronous, the community devised the callback functions, which would fire (or run) after the first function (to which the callbacks were assigned) run is completed.

or

callback is a piece of executable code that is passed as an argument to other code. When the function is done with its work , it calls your callback function.

Callback Example:

 fs.readFile(‘index.html’, function(err, data) {
    res.writeHead(200, {‘Content-Type’: ‘text/html’});
    res.write(data);
    res.end();
  });

What is callback hell?

callback hell nothing but nested callbacks.The code can get messy when there are several functions. This situation is called what is commonly known as a callback hell.
example:

firstFunction(args, function() {
secondFunction(args, function() {
thirdFunction(args, function() {
// And so on…
});
});
});

Solutions to callback hell

There are four solutions to callback hell:

  1. Write comments
  2. Split functions into smaller functions
  3. Using Promises
  4. Using Async/await