Lesson 16 / 28
Callbacks & Error-First Convention
Node's original async style, and why it can get messy.
Error-first callbacks
Node's convention: a callback's first argument is an error (or null), the rest are results.
import fs from "node:fs";
fs.readFile("notes.txt", "utf8", (err, data) => {
if (err) {
console.error("Failed:", err.message);
return;
}
console.log(data);
});Callback hell
Nesting callback inside callback inside callback grows a pyramid of doom that's hard to read and error-prone. Promises and async/await fix this.