什么是回调地狱
What is callback hell
一句话:异步一步依赖上一步时, 回调套回调,缩进越来越深, 形成一个横着的三角形 —— 也叫「厄运金字塔」。
它真正的问题不只是难看,有三条:
- 错误处理要写 n 遍。每一层都得判
if (err), 漏一个就静默失败。 - 没法组合。想改成「这两步并行」几乎要重写。
- 控制流全靠缩进表达, 加一步要改一堆括号。
怎么解,按历史顺序:命名函数拆平 → Promise 链 (把嵌套变成链式,错误集中到一个.catch)→async/await(彻底变线性)。
会追问:「Promise 解决了回调地狱的哪个问题?」——主要是「错误处理」和「组合」, 缩进只是顺带。 能这么答说明你理解本质, 而不是「Promise 让代码变平了」这种表面回答。
In one line: when each async step depends on the last, callbacks nest inside callbacks, the indentation keeps growing, and you end up with a sideways triangle — also called the pyramid of doom.
The real damage is not that it looks bad. There are three problems:
- Error handling gets written n times. Every level needs its own
if (err), and one missed check is a silent failure. - Nothing composes. Making two of those steps run in parallel means rewriting nearly all of it.
- Control flow is expressed only by indentation, so inserting a step means shuffling a pile of braces.
The fixes, in historical order: pull the callbacks out into named functions → a Promise chain (nesting becomes chaining, errors collapse into one .catch) → async/await, which makes it fully linear.
Follow-up: “Which part of callback hell did Promises actually solve?” — mostly error handling and composition; the indentation came along for the ride. Answering that way shows you understand the substance, not just “Promises flatten the code”.