Non-Blocking I/O (Node.js)

Non-blocking I/O means Node.js starts an I/O operation (file read, network request) and continues running other code instead of waiting for it to finish. The result arrives later via a callback, Promise, or await.

Node.js

In a blocking model, a thread that reads a file sits idle until the disk responds. Node instead uses non-blocking, asynchronous I/O: it tells the OS to do the work, returns immediately, and is notified when the data is ready. One thread can therefore juggle many in-flight operations.

This is what makes Node efficient for I/O-bound servers — APIs, proxies, real-time apps — where most time is spent waiting on databases and networks, not crunching numbers. While one request waits on the database, the event loop serves others.

Most Node APIs come in both flavors: the async, non-blocking version (fs.readFile, or fs.promises) and a blocking *Sync version (fs.readFileSync). Use the synchronous ones only at startup or in scripts; in a server, a sync call freezes the whole process for every connected client.

Syntax

fs.readFile(path, cb)   // async, non-blocking

Examples

JavaScript

const fs = require("fs");

fs.readFile("data.txt", "utf8", (err, data) => {
  console.log("file read finished");
});
console.log("this prints first");

Output:

this prints first
file read finished

Learn it in a lesson

See also

Back to the Dictionary