Nonblocking I/O
Local practice: Node.js 22.16.0 on Linux x64; proposed teaching baseline 26.13.1 remains untested. Copy only the listed files into a fresh disposable directory. No dependency installation or external services required. These are static Node.js examples, not verified ByteMind runner tasks.
Start a read and do independent work
import { readFile } from 'node:fs/promises';const pending = readFile('item.txt', 'utf8');console.log('calculate', 2 * 3);console.log('read', await pending);Run:
node main.mjsExpected stdout:
calculate 6read bookExit status: 0. Expected stderr: empty.
How it works
A promise-based filesystem read starts work and returns a promise; the surrounding code can perform independent calculations before awaiting the result. A synchronous read returns the data directly and subsequent code cannot run until it completes. Node may use asynchronous OS facilities or a worker pool depending on the operation. Asynchronous I/O is not necessarily faster for one tiny operation; its benefit is avoiding unnecessary blockage of the JavaScript execution thread.
Compare a synchronous read
import { readFileSync } from 'node:fs';const text = readFileSync('item.txt', 'utf8');console.log('read', text);console.log('calculate', 2 * 3);Run:
node main.mjsExpected stdout:
read bookcalculate 6Exit status: 0. Expected stderr: empty.
Common mistake
Avoid readFileSync in request paths that must serve other clients concurrently. Startup scripts and small one-shot tools can sometimes reasonably use synchronous I/O. Do not infer throughput from these tiny examples; they compare observable control flow, not speed.
Practice
Replace the asynchronous read with a synchronous read and predict the ordering. Explain which approach you would choose in a long-lived API server and why.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Nonblocking I/O — knowledge check
Apply the concepts and predict the behavior taught in this lesson.
Which call holds the JavaScript thread until the read finishes?
What is a key benefit of asynchronous I/O in a server?
What does this example establish about throughput?