Callback Hell & Async Patterns

Difficulty: Intermediate

Question

What is callback hell in JavaScript? Why is it a problem, and how do Promises and async/await solve it?

Answer

Callback hell, also called the pyramid of doom, is what happens when each asynchronous step is nested inside the callback of the previous one, so the code drifts rightwards and error handling is repeated at every level. Promises fix it by returning an object representing the future result, which can be chained flatly with one .catch(). async/await goes further and lets you write the same Promise logic as straight-line code with try/catch.

Why nested callbacks hurt

The indentation is the visible symptom, but the real problems are deeper:

How Promises flatten the code

A Promise is a placeholder for a value that is pending, then either fulfilled or rejected. .then() returns a new Promise, and if your handler returns a Promise, the chain waits for it. That is why a chain stays flat. Any rejection or thrown error skips ahead to the nearest .catch(), giving you one place for error handling. Combinators like Promise.all, allSettled, race and any handle parallel work that callbacks made painful.

async/await on top of Promises

An async function always returns a Promise. Inside it, await pauses that function (not the thread) until the Promise settles, then resumes with the value or throws the rejection reason. You get normal try/catch/finally, normal loops and normal variables across steps, which is why it is the default style in 2026 codebases.

CallbacksPromise chainasync/await
ShapeNestedFlat chainSequential lines
ErrorsChecked at every levelOne .catch()try/catch
Parallel workManual countersPromise.allawait Promise.all
GuaranteesNoneSettles once, asyncSame as Promises

Mistakes that bring the pain back

Callbacks, promises and await on the event loop. The same work scheduled three ways. Rearrange the steps under Tweak and watch the order change.
  1. A setTimeout callback is a task: It waits for the task queue, after all microtasks.
  2. A promise callback is a microtask: It runs as soon as the call stack empties.
  3. await resumes as a microtask: async/await is promise scheduling written as straight-line code.
  4. Output: async, sync, promise, async resumed, callback.

Code examples

From callback pyramid to Promise chain

// Callback hell: error checks repeated at every level
getUser(userId, (err, user) => {
  if (err) return handleError(err);
  getOrders(user.id, (err, orders) => {
    if (err) return handleError(err);
    getPaymentStatus(orders[0].id, (err, status) => {
      if (err) return handleError(err);
      console.log('Latest payment:', status);
    });
  });
});

// Same flow with Promise-returning versions: flat, one catch
getUser(userId)
  .then((user) => getOrders(user.id))          // returning a Promise keeps it flat
  .then((orders) => getPaymentStatus(orders[0].id))
  .then((status) => console.log('Latest payment:', status))
  .catch(handleError);                           // catches a failure from any step

Each then returns a new Promise, so returning the next async call flattens the chain. A single catch replaces three manual error checks.

async/await with parallel work and promisify

import { promisify } from 'node:util';
import { readFile } from 'node:fs/promises';   // built-in Promise API
import legacyGeo from './legacy-geo.js';        // old callback-style library

const lookupPincode = promisify(legacyGeo.lookup); // (pin, cb) -> Promise

async function buildOrderSummary(userId) {
  try {
    const user = await getUser(userId);

    // Independent calls: start together, wait once (no waterfall)
    const [orders, address] = await Promise.all([
      getOrders(user.id),
      lookupPincode(user.pincode),
    ]);

    const template = await readFile('./summary.txt', 'utf8');
    return template.replace('{name}', user.name).replace('{count}', orders.length);
  } catch (err) {
    console.error('Summary failed:', err.message); // any step's rejection lands here
    throw err;
  }
}

await reads like synchronous code while Promise.all keeps independent requests parallel. promisify converts an error-first callback API into a Promise-returning one.

Key points

Common follow-up questions

Interview tips

Concepts covered

Callback Hell, Inversion of Control, Promises, Async/Await, Error-first Callbacks

More JavaScript Interview questions