Promise filesystem APIs
Local practice: Node.js 22.16.0 and npm 10.9.2 on Linux x64; proposed teaching baseline 26.13.1 LTS remains untested. Use a fresh disposable folder for each example, copy only the shown files and run the fixed command. No packages or external services required. Static examples are not ByteMind runner support. The archive is author-only; do not copy expected.json into the example directory.
Sequence a write and a read
import { mkdtemp, writeFile, readFile, unlink, rmdir } from "node:fs/promises";import { join } from "node:path";const dir = await mkdtemp("async-report-");const file = join(dir, "stock.json");try { await writeFile(file, JSON.stringify({ count: 6 }), { flag: "wx" }); const data = JSON.parse(await readFile(file, "utf8")); console.log(data.count);} finally { try { await unlink(file); } catch (e) { if (e.code !== "ENOENT") throw e; } await rmdir(dir);}Run in this fixture directory:
node main.mjsExpected stdout:
6Expected exit status: 0. Expected stderr: empty. Await ensures the write finishes before the read begins; JSON.parse runs after text is available.
How it works
readFile and writeFile accept paths and encoding options much like their synchronous counterparts but return promises. A rejected operation propagates through await and can be caught with try/catch. Sequence dependent writes and reads explicitly: if a read needs a completed write, await that write first. Concurrent modifications to one file are not made safe merely by returning promises. Full-file APIs load the contents into memory; use bounded data and later consider streams for large inputs. The examples work in disposable directories and have no external dependencies.
Handle a missing optional report
import { readFile } from "node:fs/promises";async function readOptional() { try { return await readFile("optional-demo.txt", "utf8"); } catch (error) { if (error.code === "ENOENT") return "No report"; throw error; }}console.log(await readOptional());Run in this fixture directory:
node main.mjsExpected stdout:
No reportExpected exit status: 0. Expected stderr: empty. The expected missing-file rejection is translated into a documented default; other errors propagate.
Common mistake
Do not catch every failure and return an empty result. A missing optional file may justify a default, but denied access or corrupt data should be surfaced to the caller.
Try it yourself
Write a small JSON object, read it asynchronously, parse it, and reject a malformed value. Test a missing optional path separately from a parse failure.
Further reading: Official documentation 1. Related ByteMind reference candidate: node-fs-promises-readfile (route not verified).
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Promise filesystem APIs — knowledge check
Apply the concepts and predict the behavior taught in this lesson.
What does fs/promises readFile return before completion?
How should a dependent read follow a write?
Does async file access prevent all races?