Node.jsQuestion 06 of 26
Callback hell
Answer out loud or in your head first, then compare.
Answer
It's a layout of many nested callbacks in which dependencies, errors and control flow are hard to follow.
JavaScript
// Outline of the problem
loadUser(id, (error, user) => {
if (error) return handleError(error);
loadOrders(user.id, (error, orders) => {
if (error) return handleError(error);
renderOrders(orders);
});
});Solution: I extract named functions and, where the API allows, use Promises and async/await. I keep error handling at the right level.
Pitfall: callbacks themselves aren't the problem. The problem is composition that's hard to maintain, and errors that get skipped.
How did it go? Be honest — “Not yet” puts the question on your review list.