Callbacks (Why They Still Matter)
On this page
What Is a Callback?
A callback is a function passed into another function to be executed later, usually after an asynchronous operation completes.
Classic Node Callback Pattern
fs.readFile('file.txt', (err, data) => {
if (err) {
console.error(err);
return;
}
console.log(data.toString());
});
Error-First Convention
Node adopted the “error-first callback” convention: the first parameter is always an error object.
Callback Hell
step1((err, result1) => {
step2(result1, (err, result2) => {
step3(result2, (err, result3) => {
console.log(result3);
});
});
});
Problems
- Deep nesting
- Hard error propagation
- Reduced readability
Production Insight
Callbacks still exist in low-level APIs, but modern backend code prefers promises and async/await.