Learning navigation

Code with Animation

Callbacks & Callback Hell

Travel back in time to see how JavaScript originally handled asynchronous tasks, and learn why the dreaded 'Pyramid of Doom' forced the language to evolve.

JavaScript6 min readConcept 31 of 63

The Original Async Architecture

Callback-based APIs were a common way to receive asynchronous results before Promise-based APIs and async/await became widespread.

A callback is a function passed to another function for that function to call. It may run synchronously during the call, or asynchronously later; the API determines when it runs.

The Error-First Pattern

Many Node.js callback APIs follow the error-first convention.

In an error-first API, the first argument reports an error, with null on success; later arguments carry the result. Other APIs may use promises, events or different callback contracts.

Check the error argument before using the result, and return after forwarding a failure so later tasks do not run with missing data.

The Pyramid of Doom

Callbacks worked perfectly fine for a single isolated task. But if you needed to perform tasks in a strict sequence, things got ugly.

If you needed to fetch a user, then fetch their posts, then fetch the comments for those posts, you had to nest callbacks inside of callbacks inside of callbacks.

This horizontal marching of code to the right became notoriously known as Callback Hell or the Pyramid of Doom.

Follow a real callback chain

Run local timed callbacks, inject a failure at any step and cancel or replace the run while inspecting the actual delivery order.

// Local timed callback APIs
loadUser((err, user) => {
  if (err) return fail(err);
  loadPosts(user.id, (err, posts) => {
    if (err) return fail(err);
    loadComments(posts[0].id, (err, comments) => {
      if (err) return fail(err);
      // Show the actual returned data.
    });
  });
});

user

Not delivered

posts

Not delivered

comments

Not delivered

Run status: idle. Pending timers: 0.

    The next task is scheduled only after the previous callback supplies data. Callbacks can also be synchronous; this lab specifically demonstrates a timed asynchronous sequence.

    Actual local timers deliver these error-first callbacks; no network requests occur. A failure stops the remaining sequence. Cancelling clears this run's timers. Reduced motion changes presentation, not asynchronous ordering.

    Check yourself

    Pick an answer to lock it in, then read why. Getting one wrong is part of how it sticks.

    1. 1What is the defining characteristic of the Node.js 'error-first' callback pattern?
    2. 2Why is 'Callback Hell' often called the 'Pyramid of Doom'?
    3. 3Are callbacks completely obsolete in modern JavaScript?

    Remember this

    • A callback is passed to another function and may be called synchronously or asynchronously.
    • Callback-based APIs are a common pattern for receiving asynchronous results.
    • Node.js established the 'error-first' pattern: (err, data) => {}.
    • Sequential async operations required nesting callbacks inside callbacks, creating the 'Pyramid of Doom'.
    • An outer try/catch does not catch a later asynchronous throw; callback errors must be handled or forwarded at the appropriate step.

    Done with this concept?

    Mark it complete to track your progress. No login needed.