SkillByAIOpen interactive version →

Lesson 16 / 30

Callbacks & Error-First Convention

Understand error-first callbacks, Node's original async style, and the pyramid of doom they can create.

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.