
Callback Hell and Pyramid of Doom in JavaScript
Callback Hell happens when you have too many nested callbacks, making your code:
Example: Callback Hell
Imagine youβre doing 3 things in order:
Each task takes time (simulated by setTimeout), and we use callbacks to make sure they happen in order.
Example:-
setTimeout(() => {
console.log(" Ordered pizza");
setTimeout(() => {
console.log(" Made a drink");
setTimeout(() => {
console.log(" Started the movie");
}, 1000); // watch movie
}, 1000); // make drink
}, 1000); // order pizzaOutput:-
Ordered pizza
Made a drink
Started the movieThe output is correct β but look at the code! It's deeply nested and becomes messy fast!
Why You Should Avoid Callback Hell

How to Fix Both?
function watchMovie() {
setTimeout(() => {
console.log(" Started the movie");
}, 1000);
}
function makeDrink() {
setTimeout(() => {
console.log(" Made a drink");
watchMovie();
}, 1000);
}
function orderPizza() {
setTimeout(() => {
console.log(" Ordered pizza");
makeDrink();
}, 1000);
}
orderPizza();