Difficulty: Intermediate
What is callback hell in JavaScript? Why is it a problem, and how do Promises and async/await solve it?
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.
The indentation is the visible symptom, but the real problems are deeper:
(err, data) => {}, every level needs if (err) return .... Miss one and the error disappears silently.throw inside an async callback cannot be caught by a try/catch around the original call.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.
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.
| Callbacks | Promise chain | async/await | |
|---|---|---|---|
| Shape | Nested | Flat chain | Sequential lines |
| Errors | Checked at every level | One .catch() | try/catch |
| Parallel work | Manual counters | Promise.all | await Promise.all |
| Guarantees | None | Settles once, async | Same as Promises |
await a(); await b(); runs sequentially even when b does not need a. Start both and await Promise.all([a(), b()])..then inside .then. Return the inner Promise instead.await or return. The function moves on with a pending Promise and errors become unhandled rejections.await inside forEach. forEach ignores the returned Promises; use for...of for sequence or Promise.all(items.map(...)) for parallel.new Promise or Node's util.promisify, and prefer the built-in Promise versions such as fs/promises.// 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.
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.
Callback Hell, Inversion of Control, Promises, Async/Await, Error-first Callbacks