The Event Loop, in Server Terms
Node runs your JavaScript on one thread, like one waiter serving many tables, so it serves many requests at once by starting slow file and network work and moving on while it waits, and a long CPU-heavy loop makes every user wait.
Journeyman · Node.js · JavaScript · Updated
After this lesson you can
- Explain how Node's event loop serves many requests at once by starting slow file and network work and moving on while it waits
- Explain why a long CPU-heavy loop or a Sync function blocks every user of a Node server
Key takeaways
- Node runs your JavaScript on one thread, one thing at a time, like a restaurant with one waiter.
- When code waits for a file, a database or the network, Node starts the work and moves on to the next request. That's non-blocking I/O.
- When the slow work is done, the rest of its code is queued, and the event loop runs it as soon as the thread is free.
- A long loop that keeps the thread busy, or a Sync function such as readFileSync, blocks every request until it's done.
- Behind the scenes, a small pool of helper threads does the file work, and worker_threads can move heavy calculations off the main thread.
From Module 2 on, you'll build servers that answer many people at once. A Node server can handle thousands of requests with a single thread of JavaScript, and it can also freeze for every one of them because of one slow loop. Knowing which, and why, is how you'll spot the cause when an API suddenly slows down for everyone, and it's a favorite interview question.
One waiter, many tables
Picture a small restaurant with one waiter. A slow waiter would take one table's order, stand at the kitchen door until the food is ready, carry it out, and only then go to the next table. A good waiter takes an order, hands it to the kitchen, and goes straight to the next table. When a dish is ready, the kitchen rings a bell, and the waiter carries it out at the next free moment.

Node is the good waiter. It runs your JavaScript on one thread, one line of work, so only one piece of your code runs at a time. A server's requests are the tables. Most of a request's time is spent waiting: for a file, a database or another server to answer. Those are the kitchen's jobs, called I/O (read "I-O", input and output). When your code awaits one, Node starts the work and moves on to the next request. That's non-blocking I/O. When the work is done, the rest of that function is put in a queue, and the event loop, Node's loop that runs whatever is ready next, runs it as soon as the thread is free.
Watching it: three requests at once
The club's server will answer requests for a runner's week, and each needs that runner's log from a slow disk or database. Here, wait(ms) from node:timers/promises stands in for that slow read, so you can see the timing:
import { setTimeout as wait } from "node:timers/promises";
// Each request needs a runner's log from a slow disk or database.
// wait(ms) stands in for that slow read: Node starts it and moves on.
async function handle(runner, ms) {
console.log(`Took ${runner}'s request`);
await wait(ms);
console.log(`Sent ${runner}'s week (${ms} ms)`);
}
const start = Date.now();
await Promise.all([handle("Maya", 300), handle("Leo", 100), handle("Priya", 200)]);
console.log(`All three answered in about ${Math.round((Date.now() - start) / 100) * 100} ms`);
Took Maya's request
Took Leo's request
Took Priya's request
Sent Leo's week (100 ms)
Sent Priya's week (200 ms)
Sent Maya's week (300 ms)
All three answered in about 300 ms
Promise.all starts all three and waits until every one has finished. Node took all three requests before any read was done, then answered each as its read came back, fastest first. The three reads overlapped, so the whole thing took about 300 ms, the slowest one, not 600 ms, the three added up.
What blocks everyone: a long loop
The waiter can't take orders while they're busy doing something themselves. Here, Leo asks for his whole year's report, and the code adds it up in a loop that keeps the thread busy for two seconds (busyFor stands in for any long calculation):
import { setTimeout as wait } from "node:timers/promises";
function busyFor(ms) {
const end = Date.now() + ms;
while (Date.now() < end) {
// Adding up a year of runs, the slow way: Node can't do anything else.
}
}
async function handle(runner, ms) {
console.log(`Took ${runner}'s request`);
await wait(ms);
console.log(`Sent ${runner}'s week`);
}
async function handleYearReport(runner) {
console.log(`Took ${runner}'s year report`);
busyFor(2000);
console.log(`Sent ${runner}'s year report`);
}
const start = Date.now();
await Promise.all([handle("Maya", 100), handleYearReport("Leo"), handle("Priya", 100)]);
console.log(`Maya waited about ${Math.round((Date.now() - start) / 1000)} seconds for a 100 ms read`);
Took Maya's request
Took Leo's year report
Sent Leo's year report
Took Priya's request
Sent Maya's week
Sent Priya's week
Maya waited about 2 seconds for a 100 ms read
Maya's read finished after 100 ms, but nothing could carry it out: the thread was inside Leo's loop. Priya's request wasn't even taken until the loop ended. On a real server, every user would wait those two seconds. That's blocking code: code that keeps the thread busy, so nothing else runs.
The same goes for Node's Sync functions, such as readFileSync: they wait for the file with the thread stopped. They're fine in a short script that does one thing and ends, and wrong inside a server's request handler. That's why this course awaits readFile from node:fs/promises.
Where the waiting happens
If your JavaScript runs on one thread, who reads the files? Node's own engine room, a library named libuv, keeps a small pool of helper threads (four, unless you change it) for file work, and lets the operating system watch the network, so all that waiting happens off your thread.
That pool isn't for your calculations. When a server truly needs heavy work, such as resizing photos or a year of statistics, Node's worker_threads module runs it on a thread of its own, so the event loop stays free; Work That Runs After the Response, in Module 5, puts it to use. Until then, the rule is short: await your I/O, and keep long loops out of request handlers.
Check yourself
2 questions to try on the lesson page.
So what
One thread serves everyone, so a Node server is fast while each request mostly waits and the waiting is awaited; give it one long loop, and every user waits behind it.