⬅ Previous Topic
JavaScript Modules - import & exportNext Topic ⮕
JavaScript Promises - Creating and Chaining⬅ Previous Topic
JavaScript Modules - import & exportNext Topic ⮕
JavaScript Promises - Creating and ChainingJavaScript is single-threaded and often performs asynchronous operations like API calls, timers, or file reading. To handle such operations without blocking the main thread, callbacks are used.
A callback is a function passed as an argument to another function, which is then invoked inside that function after some operation is complete.
function greetUser(name, callback) {
console.log("Hello " + name);
callback();
}
function sayBye() {
console.log("Goodbye!");
}
greetUser("Alice", sayBye);
Hello Alice Goodbye!
JavaScript executes code line by line, but asynchronous operations (like network requests) don’t complete instantly. Callbacks ensure that a task runs only after the previous one finishes.
function fetchData(callback) {
setTimeout(() => {
console.log("Data fetched from server.");
callback();
}, 2000);
}
function processData() {
console.log("Processing data...");
}
fetchData(processData);
Data fetched from server. Processing data...
Let’s simulate a real-world example: logging in, fetching user profile, then fetching posts.
function loginUser(callback) {
setTimeout(() => {
console.log("User logged in");
callback();
}, 1000);
}
function fetchProfile(callback) {
setTimeout(() => {
console.log("User profile fetched");
callback();
}, 1000);
}
function fetchPosts() {
setTimeout(() => {
console.log("User posts fetched");
}, 1000);
}
loginUser(() => {
fetchProfile(() => {
fetchPosts();
});
});
User logged in User profile fetched User posts fetched
When multiple asynchronous operations are nested within each other in this way, it creates deeply nested code that is hard to read, debug, and maintain. This is known as Callback Hell or the “Pyramid of Doom”.
Callback hell makes the code look like this:
task1(() => {
task2(() => {
task3(() => {
task4(() => {
// and so on...
});
});
});
});
Answer: Because functions like setTimeout
or fetch
are asynchronous. If you don’t use callbacks (or promises), the next function might execute before the previous one finishes.
Solutions include:
We will explore Promises and async/await in upcoming modules to resolve this issue effectively.
⬅ Previous Topic
JavaScript Modules - import & exportNext Topic ⮕
JavaScript Promises - Creating and ChainingYou can support this website with a contribution of your choice.
When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.