What Is A Callback Function?
// calling the notify function
// passing notify function
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.
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).
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
Writing the callback function for concept 2:
// Make dinner
// code to make dinner
console.log("Dinner is ready");
// text her when you are HOME
// Texting to Mrs. Node
console.log("SMS sent to Mrs.Node");
// it may take 20-30 minutes to go home
// code to go home
// arrived at home
Execution Of Callback 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’
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.
return console.log("DB operation failed");
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.