Skip to content
  • Callbacks & Callback Hell · JavaScript

Callbacks & Callback Hell

LearnViaNotes @learnvianotes· 2 min read

The original async pattern: pass a function to be called when the work is done. Simple — until you nest five of them.

Error-first callbacks (Node convention)

JavaScript
function readUser(id, callback) {
  setTimeout(() => {
    if (id <= 0) callback(new Error("invalid id"));
    else callback(null, { id, name: "User" + id });
  }, 10);
}
readUser(7, (err, user) => {
  if (err) return console.log("failed:", err.message);
  console.log("got", user);
});
readUser(-1, (err, user) => {
  if (err) return console.log("failed:", err.message);
  console.log("got", user);
});
Output
got { id: 7, name: 'User7' }
failed: invalid id

Callback hell (the pyramid of doom)

JavaScript
const step = (name, cb) => setTimeout(() => cb(null, name), 5);
step("login", (err, a) => {
  step("load profile", (err, b) => {
    step("load orders", (err, c) => {
      step("render", (err, d) => {
        console.log([a, b, c, d].join(" → "));
      });
    });
  });
});
Output
login → load profile → load orders → render
ProblemWhy it hurts
NestingCode drifts right; hard to read and refactor
Error handlingMust check err at every level; try/catch cannot catch errors thrown in later callbacks
Inversion of controlYou trust the callee to call you exactly once, not zero or twice, not sync and async
CompositionParallel work, timeouts and "first one wins" need hand-written bookkeeping

Promisify

JavaScript
import { promisify } from "node:util";
function legacyAdd(a, b, cb) { setTimeout(() => cb(null, a + b), 5); }

const addAsync = promisify(legacyAdd);
console.log(await addAsync(2, 3));

const manual = (a, b) =>
  new Promise((resolve, reject) => legacyAdd(a, b, (err, v) => (err ? reject(err) : resolve(v))));
console.log(await manual(10, 20));
Output
5
30

What is callback hell and how do you avoid it?

Free for students and teachers

Write notes like this one

Cards for every idea, quizzes and flashcards, read aloud — on the web and your phone.

Something wrong with this note? Open it to report