What Is A Callback Function?
A callback function is a function that would be passed into another function as an argument. In Javascript, functions are actually first-class objects. Just like you can pass an object into a function as an argument, you can pass other functions into a function as an argument. In short, Callback is passing a function into another function.

function notify(){
  console.log("task finished");

function taskOne(callback){
  // calling the notify function

// passing notify function
Callback Concept
Callback Concept 1
Let’s think of a restaurant which has a chef and the restaurant has only one server to serve the users. When a customer arrives, the server is going to listen to the request of the customer for their order and he’s going to the chef and say, hey a customer has ordered a pizza, can you start preparing it. Let’s say chef takes about 5-10 minutes to cook the pizza. Now we have two scenarios

The first scenario is that the server is going to wait for five 5-10 minutes doing nothing and once the pizza is ready the server is going to bring it back to the customer. Meanwhile, any customer that arrives would have to wait because our server is not going to READ for another customer until the first customer is served
The better scenario is when the first customer arrives the server is going to take his request, go to the chef and say, chef, we have an order. Now I am going to listen to other customers until the pizza is ready. Once the pizza is ready CALL me BACK. So that I don’t have to wait for another 5-10 minutes to hear the request of another customer.
Callback Concept 2
Consider your partner going home early today and you want him/her to do something when he/she arrives. And you are still at the office doing you doing.

Choosing Best NodeJS Frameworks

Here are two scenarios:

1. Synchronous way, where you have to wait until the task is not done.


/* instruct partner to do do something
 * - let's say, ask him to go home
 * - takes 20 to 30 minutes to go home

/* continue work after 20-30 minutes */
2. Asynchronous way, you don’t have to wait for the result. When the task is done, let it know what to do next. Here is a quick example of using a callback

/* Notify me when task is done */

/* OR: do other thing when this job is done */

// full code is available below
The first example shows that the program blocks until your partner goes home and text you, you can not continue your office work.
The second example shows that the program does not wait for his task to finish. you can continue your work. As soon as your partner finish the first task he will do the next task as per your instruction
“Callbacks” – It’s the Hollywood principle: Don’t call me, I’ll call you.
So instead of calling the studio and constantly inquiring whether they have an opening (polling) they call you when they have one (call back). 
?— Quora

The main concept of the callback is not to wait for something finish, rather let it get back to you with the results

Writing A Callback Function
There are no new rules for writing a callback function in javascript. You can define a regular function and pass it over OR you can create an anonymous function.

An introduction to the basic principles of Functional Programming

Writing the callback function for concept 2:


// Make dinner
function makeDinner(){
  // code to make dinner
  console.log("Dinner is ready");

// text her when you are HOME
function textMe(){
  // Texting to Mrs. Node
  console.log("SMS sent to Mrs.Node");

// it may take 20-30 minutes to go home
function goToHome(doNext){
  // code to go home
  // arrived at home
Execution Of Callback Function
Execution in JavaScript are traditionally synchronous and it executes sequentially from top to bottom. But in an asynchronous environment, execution sequence doesn’t not follow line by line


setTimeout(function() {


Output: One, Two, Three
Note: The callback function will not run until it is being called by its containing function. Hence, the term is call back function

Obviously, you can call the callback function any time you want it in the code. But the idea behind in callback function is to notify the result from the task function. So please make sure you invoke the callback at the end of the function

How Node.JS Comes Into The ‘Callback Function’
A callback function is used to enforce the order of operations in an asynchronous environment and Node.JS is quite popular for its async behavior. The callback function is not a feature of Node.JS but the javascript itself. Node.JS uses this features heavily. Native APIs of Node.JS have two version of implementations, Sync and Asycn. All async functions of Node.JS have callbacks at the end of the function parameters.

Many people have a misconception about the callback function. As many newbies start knowing the terms in Node environments, few of people thinks it is a feature of Node.JS

Callback Parameters In Node.JS
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 writing a callback function in Node.JS then it’s recommended to keep the first argument for errors and second argument for the result.

getEmailsFromDB(function(err, result){
    return console.log("DB operation failed");

function getEmailsFromDB(callback){
    return callback(new Error("Connection Error"));
  const emails = fetch(); // db query
  return callback(null, emails);
Understanding the concepts of Callbacks is confusing sometimes but they come in handy a lot in event-driven programming or when making API requests.

Node.Js Roadmap – A Route From Beginners To Become Expert Developer