Three Files, Three if (err) Checks: Callbacks vs Promises
Why async code exists in Node.js, error-first callbacks, nested callback problems, fs/promises, and when callbacks still fit

Reading one file in Node.js with a callback takes five lines. Reading three files where each file names the next turns into a staircase of indentation and three separate if (err) checks.
That staircase is the exact reason Node.js evolved from callbacks to Promises. Every output below comes from a real run on Node.js 24.22.1. To run the snippets locally, create three files in the same folder: user.txt containing order-101.txt, order-101.txt containing receipt-101.txt, and receipt-101.txt containing Paid: 450.
Why async code exists in Node.js
Your JavaScript in Node.js runs on one main thread. Disk reads, database queries, and HTTP calls are slow compared to CPU instructions. If Node.js paused that single thread every time it read a file, a web server handling dozens of users would freeze for everyone while waiting on one disk read.
Asynchronous code lets Node.js start a slow I/O task, keep executing the rest of your script, and run a follow-up handler once the OS finishes the task.
Callback-based async execution, step by step
A callback is a function you pass as an argument to another function so it can be called later when the work finishes. Node.js uses an error-first callback convention — the first parameter is always err (or null if everything succeeded), and the second is the result. Putting err first is deliberate: wrapping fs.readFile(...) in a synchronous try/catch block will not catch an error that happens later inside the callback, so Node passes errors through the first argument instead:
const fs = require("fs");
console.log("1. Requesting user.txt");
fs.readFile("user.txt", "utf8", (err, data) => {
if (err) return console.error("Read failed:", err.code);
console.log("3. Got file:", data.trim());
});
console.log("2. Main thread keeps running");
1. Requesting user.txt
2. Main thread keeps running
3. Got file: order-101.txt
Step by step:
- Line 1 prints
"1. Requesting user.txt". fs.readFilehands the disk read to libuv's background worker pool and returns immediately.- Line 2 prints
"2. Main thread keeps running"before the disk read has finished. - When the read completes, Node.js queues your callback on the event loop, and it prints
"3. Got file: order-101.txt".
For a single independent operation, callbacks are short and clear. The pain begins when one async step depends on the previous step's result.
Problems with nested callbacks
Suppose user.txt holds the name of an order file (order-101.txt), which holds the name of a receipt file (receipt-101.txt). With callbacks, each read must sit inside the previous callback:
// ❌ nested callbacks: 3 indentation levels and 3 repeated error checks
const fs = require("fs");
fs.readFile("user.txt", "utf8", (err, orderFile) => {
if (err) return console.error("Step 1 failed:", err.code);
fs.readFile(orderFile.trim(), "utf8", (err, receiptFile) => {
if (err) return console.error("Step 2 failed:", err.code);
fs.readFile(receiptFile.trim(), "utf8", (err, receipt) => {
if (err) return console.error("Step 3 failed:", err.code);
console.log("Receipt:", receipt.trim());
});
});
});
Receipt: Paid: 450
This pattern — nicknamed callback hell or the pyramid of doom — creates three real maintenance problems:
- Horizontal drift: every dependent step pushes code further right, making the control flow hard to scan.
- Repeated error handling: every single layer needs its own
if (err) return .... Forget onereturnand the next step runs withundefineddata. - Hard to compose: running three reads concurrently and waiting for all of them requires manual counters and state variables.
Promise-based async handling (fs/promises)
A Promise is an object representing a value that is not ready yet. It moves through three states: pending (work in progress), fulfilled (succeeded with a value), or rejected (failed with an error). If you want a dedicated intro to Promises first, see JavaScript Promises for beginners.
Node.js ships a Promise-based version of fs under require("fs/promises"). Here is the exact same three-file chain rewritten with .then() and a single .catch():
// ✅ flat Promise chain with one shared error handler
const fs = require("fs/promises");
fs.readFile("user.txt", "utf8")
.then((orderFile) => fs.readFile(orderFile.trim(), "utf8"))
.then((receiptFile) => fs.readFile(receiptFile.trim(), "utf8"))
.then((receipt) => console.log("Receipt:", receipt.trim()))
.catch((err) => console.error("Chain failed:", err.code, err.path));
Receipt: Paid: 450
Notice what happens when step 2 tries to read a missing file. To see it, change user.txt to contain missing.txt, then run the same chain:
Chain failed: ENOENT .../missing.txt
The rejection skips the remaining .then() handlers and lands straight in the single .catch() at the bottom. You do not repeat if (err) three times.
Two small Promise details save hours of debugging:
- Always return the inner Promise inside
.then(). Notice our arrow functions omit curly braces ((orderFile) => fs.readFile(...)), which returns the inner Promise implicitly. If you add{ ... }braces and forgetreturn, the next.then()runs immediately withundefinedinstead of waiting for the file read to finish. - Use
.finally()for cleanup. Code inside.finally(() => ...)runs whether the chain fulfilled or rejected — handy for closing a connection or logging that a job ended.
Benefits of Promises over callbacks
| Aspect | Callbacks (fs) |
Promises (fs/promises) |
|---|---|---|
| Chaining dependent steps | Nests deeper at every step | Stays flat with .then() |
| Error handling | Separate if (err) at every level |
One .catch() for the whole chain |
| Running tasks concurrently | Manual counters & result arrays | Built-in Promise.all([...]) |
| Modern syntax support | Cannot await directly |
Works directly with async/await |
Look how clean concurrent reads become when three files do not depend on each other:
const fs = require("fs/promises");
Promise.all([
fs.readFile("user.txt", "utf8"),
fs.readFile("order-101.txt", "utf8"),
fs.readFile("receipt-101.txt", "utf8"),
])
.then((contents) => console.log(contents.map((c) => c.trim())))
.catch((err) => console.error("Failed:", err.code));
[ 'order-101.txt', 'receipt-101.txt', 'Paid: 450' ]
Promise.all starts all three reads at once and preserves their order in the returned array (if any one read rejects, Promise.all rejects immediately; use Promise.allSettled when you want results from every file even if one fails). And if you ever need to call an older callback-only Node API, Node's built-in util.promisify(fn) wraps an error-first callback function into a Promise-returning one. Once you are comfortable with .then() chains, from .then() chains to async/await shows how async/await makes Promise code read like synchronous lines.
When NOT to replace callbacks with Promises
A Promise settles exactly once — it resolves or rejects a single time and is done. That makes Promises the wrong abstraction for repeating events:
- Streams and HTTP chunks:
req.on("data", (chunk) => ...)fires once per incoming chunk, not once per request. - Event emitters and timers:
server.on("request", ...)andsetInterval(...)run over and over.
Use Promises for one-shot async results (reading a file, querying a database, calling an API), and keep callbacks for event listeners and streams that fire multiple times.
Where this leaves you
Async code keeps Node's single JavaScript thread moving during slow I/O. Error-first callbacks work for one-off calls and repeating event listeners, while fs/promises and .then()/.catch() keep multi-step file and network chains flat, composable, and easy to handle when errors happen.
One honest limit: this post stopped at .then() chains rather than covering async/await syntax (linked above) or Node's unhandled Promise rejection exit behavior when you forget .catch(). Do you still run into callback-only libraries in your Node.js projects, or have you moved almost entirely to Promises and async/await?
Comments
Join the discussionNo comments yet. Be the first to comment.



