Callbacks in Node.js
A callback is a function passed as an argument to another function. It can be executed after a task is completed. Callbacks are commonly used in JavaScript to handle asynchronous operations.
1. What is a Callback?
A callback is simply a function that we pass to another function. The receiving function can call it when required.
2. Simple Callback Example
In this example, the greet() function receives another function as its callback.
function greet(name, callback) {
console.log("Hello " + name);
callback();
}
function showMessage() {
console.log("Welcome to CIIT Training Institute!");
}
greet("Samadhan", showMessage);
Output
Hello Samadhan Welcome to CIIT Training Institute!
Explanation
- greet() accepts a name and a callback function.
- showMessage is passed without parentheses because we are passing the function itself.
- Inside greet(),
callback()executes the passed function. - The message is displayed after the greeting.
3. Callback with Data
A callback can also receive data from another function. Let's use an example involving Samadhan and Pradnya.
function calculateMarks(marks, callback) {
let total = marks.reduce((sum, mark) => sum + mark, 0);
callback(total);
}
function displayResult(total) {
console.log("Samadhan's Total Marks: " + total);
}
calculateMarks([80, 85, 90], displayResult);
Output
Samadhan's Total Marks: 255
Here, calculateMarks() calculates the total and passes
the result to displayResult() through the callback.
4. Asynchronous Callback
A callback can be used with asynchronous operations.
For example, setTimeout() executes a callback after a delay.
console.log("Samadhan started learning");
setTimeout(function () {
console.log("Pradnya completed the task");
}, 2000);
console.log("Samadhan continues studying");
Output
Samadhan started learning Samadhan continues studying Pradnya completed the taskThe final message appears after approximately 2 seconds.
5. Types of Callback Usage
| Type | Description | Example |
|---|---|---|
| Synchronous Callback | Executed during the current function call. | array.map() |
| Asynchronous Callback | Executed later when an asynchronous task is ready. | setTimeout() |
| Callback with Data | Receives a result as an argument. | callback(total) |
6. What is Callback Hell?
Callback hell happens when many callbacks are nested inside one another. The code becomes difficult to read, debug, and maintain.
setTimeout(() => {
console.log("Step 1");
setTimeout(() => {
console.log("Step 2");
setTimeout(() => {
console.log("Step 3");
}, 1000);
}, 1000);
}, 1000);
7. Important Points to Remember
- A callback is a function passed to another function.
- Use
callback()to invoke the callback. - Callbacks can receive values as arguments.
- Callbacks may be synchronous or asynchronous.
setTimeout()accepts a callback function.- Promises and async/await can help organize asynchronous code.
Summary
In this lesson, we learned about callbacks, passing functions as arguments, returning data through callbacks, asynchronous callbacks using setTimeout(), and callback hell. The examples featuring Samadhan, Pradnya, and CIIT Training Institute help explain how callbacks work in practical JavaScript programs.