JavaScript Interview Question #77
What is callback hell?
Promises, Async/Await, Fetch & Modern JavaScript Mid-Level Intermediate
Quick Interview Answer
Callback hell describes deeply nested callback-based asynchronous code that becomes difficult to read, maintain, test, and handle errors in. It often appears when each asynchronous operation depends on the result of the previous operation.
Detailed Explanation
Callback hell describes deeply nested callback-based asynchronous code that becomes difficult to read, maintain, test, and handle errors in. It often appears when each asynchronous operation depends on the result of the previous operation.
Promises and async/await provide more structured alternatives. Callback-based code can also be improved by extracting named functions rather than nesting anonymous callbacks excessively.
Code Example
login(user, result => {
loadProfile(result.id, profile => {
loadOrders(profile.id, orders => {
console.log(orders);
});
});
});
// Promises or async/await usually provide a clearer flow.