← Lessons

quiz vs the machine

Silver1080

Concurrency

Callbacks And The Callback Hell

The original way to sequence async work, and why deeply nested callbacks become hard to manage.

4 min read · intro · beat Silver to climb

Passing the rest of the work

A callback is a function you hand to an async operation so it can call you back when the result is ready. Instead of returning a value, the operation accepts a function and invokes it later. This lets the thread continue while the slow work happens elsewhere.

Sequencing dependent steps

Often one async step depends on the result of the previous one. With callbacks you nest each step inside the previous callback.

  • Read a file, then in its callback parse it.
  • Inside that, query a database.
  • Inside that, write the result.

Each dependency adds another layer of nesting.

The pyramid of doom

When many steps chain this way the code drifts rightward into a deep pyramid, sometimes called callback hell. The problems are real.

  • Error handling repeats at every level, since each callback gets its own error argument.
  • Reading order no longer matches execution order, making the flow hard to follow.
  • Reuse is awkward because logic is buried inside nested functions.

These pains motivated promises and async await, which flatten the nesting.

Key idea

Callbacks let async operations report results later, but chaining dependent steps nests them deeply, producing fragile error handling and tangled flow known as callback hell.

Check yourself

Answer to earn rating on the learn ladder.

1. What is a callback?

2. Why does chaining dependent callbacks cause callback hell?

3. Which problem is common in deeply nested callbacks?