Callback Hell
IntermediateWhy steps that each wait on the one before end up nested deeper and deeper, and why that hurts.Builds on Higher-Order Functions
Empty
Empty
Empty
Empty
- Running code
- Running microtasks
- Taking the next task
- Waiting
Empty
Spaceplay or pause←→previous or next stepRback to the start
Explanation
2 min read
On this page
What is callback hell in JavaScript?
A callback is a function you hand to another function to run later. When several asynchronous steps each depend on the one before, every next step has to be written inside the previous step's callback. The code drifts right with each level, into what is called or the pyramid of doom.
Why do nested callbacks get hard to follow?
- An asynchronous function cannot return its result; it can only call you back with it.
- So "do B after A" means "put B inside A's callback", and "then C" means one level deeper.
- Errors cannot be caught by one
try…catcharound the whole thing, because each callback runs later, on its own. Every level has to check for its own error. - You give up control: the function you pass a callback to decides when, and how often, it runs.
What mistakes do people make with callbacks?
Mini example
getUser(1, (error, user) => { if (error) return console.error(error); getOrders(user.id, (error, orders) => { if (error) return console.error(error); getDetails(orders[0], (error, details) => { if (error) return console.error(error); console.log(details); }); });});Quick check
In const user = getUser(1, callback), where is the user available?
Further reading
Watch the video
The full explanation from the YouTurkey Tech YouTube channel, in Arabic.
All steps as text
- Step 1:
Three steps must happen in order: load the user, then their orders, then the order details. Each one takes a second.
- Step 2:
start is printed right away.
Line 1
- Step 3:
The first
setTimeouthands its callback to the browser's timer. Steps 2 and 3 are written inside it, because they can only start once step 1 is done.Lines 3–11
- Step 4:
The script does not wait: end is printed next.
Line 13
- Step 5:
The script is done and the call stack is empty. Everything else now waits on the timer.
- Step 6:
One second later, the first callback moves to the task queue, and from there onto the empty call stack.
Lines 3–11
- Step 7:
Step 1 prints 1. user loaded.
Line 4
- Step 8:
Step 2 needs the user, so its
setTimeoutlives inside step 1. Its callback goes to the timer, one level deeper.Lines 5–10
- Step 9:
The first callback returns. The stack is empty again.
Line 11
- Step 10:
A second later, the orders callback goes through the task queue and onto the stack.
Lines 5–10
- Step 11:
Step 2 prints 2. orders loaded.
Line 6
- Step 12:
Step 3 is nested one level further still. Each step that waits adds another level of indentation.
Lines 7–9
- Step 13:
The orders callback returns.
Line 10
- Step 14:
After one more second, the details callback reaches the stack.
Lines 7–9
- Step 15:
Step 3 prints 3. details loaded.
Line 8
- Step 16:
The last callback returns. All three steps ran in order, three seconds apart.
Line 9
- Step 17:
It works, but the code drifts right into a pyramid. Add error handling at every level and it gets harder to read. Promises flatten it.
Lines 3–11