One thread, many things going on
A web page responds to clicks while it downloads data and runs animations. A Node.js server handles thousands of connections at once. Both run your JavaScript on a single thread: one line at a time, never two at once.
How does one thread juggle all that? It never waits. When something will take a while, such as a network request or a timer, the thread hands it off, carries on with other code, and comes back when the result is ready.
The mechanism that decides what runs next is the event loop.
A puzzle
What does this print?
console.log("A");
setTimeout(() => console.log("B"), 0);
Promise.resolve().then(() => console.log("C"));
console.log("D");A timer with a delay of zero, a promise that is already resolved, and two plain logs. Most people guess A, B, C, D.
1console.log("A");2setTimeout(() => console.log("B"), 0);3Promise.resolve().then(() => console.log("C"));4console.log("D");
Waiting to run
nothing yet
Start. Four lines. Before stepping through, guess the order the letters are printed in.
The answer is A, D, C, B.
The three parts
The call stack. The code that is running right now. JavaScript can only run what is on top of the stack, and it runs it to completion. Nothing can interrupt it.
The task queue. Functions waiting for their turn: timer callbacks, click handlers, network responses that have arrived.
The microtask queue. A second, higher-priority queue. Promise callbacks (.then, and everything after an await) go here.
The loop
Run the current code until the call stack is empty
No interruptions.
Run every microtask
All of them, including any new ones added while doing so, until that queue is empty.
In a browser, update the screen if needed
Drawing happens here, between tasks.
Take one task from the task queue and run it
Then go back to step 2.
That explains the puzzle. setTimeout(..., 0) does not mean "run now". It means "put this in the task queue as soon as you can". The promise callback goes in the microtask queue. When the script finishes, microtasks go first, so C is printed before B.
And D comes before both, because the script itself has to finish before the loop looks at any queue.
Never block the loop
Because there is one thread, while your code is running, nothing else can. No clicks are handled. No timers fire. The screen is not redrawn.
Example
A frozen page
button.onclick = () => {
for (let i = 0; i < 5_000_000_000; i++) {} // several seconds of work
};Click that button and the whole page locks up. Scrolling, typing, animations: all of it waits in the task queue behind your loop. On a server, the same loop would make every other user wait.
This is why slow things in JavaScript are asynchronous. A network request is handed to the browser or the operating system, and your callback runs later as a task. The thread is free in the meantime.
What to do with genuinely heavy computation:
- Move it off the main thread. Web Workers in the browser, worker threads in Node.js.
- Break it into pieces and let the loop breathe between them.
- Do it somewhere else: on the server, or in a background job.
What await really does
await looks like it pauses. It does not block anything.
async function load() {
console.log("before");
const data = await fetch(url); // the function is suspended here
console.log("after"); // runs later, as a microtask
}At the await, the function is put aside and control returns to the event loop, which is free to run other code. When the promise resolves, the rest of the function is queued as a microtask and continues from where it left off.
So await means: "let other things run until this is ready". That is why one thread can have thousands of requests in flight. See Async/Await: In a Row or Together.
Why design it this way
| One thread with an event loop | Many threads | |
|---|---|---|
| Shared data | No locks needed: only one thing runs at a time | Needs locks, and risks race conditions |
| Waiting on the network | Excellent: thousands of connections on one thread | Works, at one thread per waiting connection |
| Heavy computation | Blocks everything | Can use several cores |
| Main danger | Blocking the loop | Races and deadlocks |
A single thread removes a whole class of bugs. It is a very good fit for work that is mostly waiting, which describes most web servers and user interfaces.
Key takeaways
- JavaScript runs your code on one thread, one piece at a time, with no interruptions.
- Waiting work is handed off, and its callback is queued for later.
- After each task, all microtasks (promise callbacks) run before the next task (timers, events).
- That is why a resolved promise runs before a zero-delay timer.
- Long-running code blocks everything, so heavy work belongs on another thread.