Kelly Mears

Event Loop

A single-threaded runtime's mechanism for pulling the next callback off a queue once the call stack is empty.

Computation & Algorithms2 min read287 words11 out · 17 in
also calledSingle-threaded event loop

Event Loop is the mechanism a single-threaded runtime uses to handle asynchronous work: a queue of pending callbacks, and a loop that pulls the next one off and runs it to completion whenever the call stack is empty. Node.js and every browser's JavaScript engine run this way — setTimeout(fn, 0) doesn't run fn immediately, it schedules fn onto the queue, and the loop won't get to it until the currently executing code finishes and the stack empties out.

The consequence that trips people up is that "asynchronous" in this model does not mean "concurrent" in the sense of running at the same time — JavaScript's event loop is single-threaded, so exactly one callback runs at any instant, and a long-running synchronous callback blocks the entire loop, freezing timers, network callbacks, and UI rendering until it returns. This is why a genuinely CPU-heavy task (image processing, a large sort) has to be moved off the main thread entirely — a Web Worker, a worker_threads instance — rather than just wrapped in a Promise, since a promise only reschedules when work runs, not where.

Node's event loop has distinct phases (timers, pending callbacks, poll, check, close) that determine ordering precisely: a setImmediate and a setTimeout(fn, 0) can fire in either order depending on which phase the loop is in when they're scheduled, which is the kind of ordering subtlety that only matters once, in a debugging session, and is otherwise safely ignorable.

The event loop is one specific answer to the concurrency-without-threads problem; the Actor Model is another, structuring concurrency around isolated units of state exchanging messages rather than a shared queue and a single thread of control. Both avoid the shared-mutable-state hazards of true multithreading, from different directions.

See also9

Concurrency and Parallelism

Concurrency is structuring independent tasks together; parallelism is actually running them at the same instant.

Computation & Algorithms8 connections

Process

A running program with its own memory, environment, and identity.

Systems & Tooling33 connections

Race Condition

A defect whose occurrence depends on the relative timing of concurrent operations.

Testing & Verification36 connections

Promise

JavaScript's object for a value that will arrive later, how it replaced nested callbacks, and the microtask queue that decides when its handlers run.

Web Platform10 connections

JavaScript

The programming language of the web browser, written in ten days in 1995, standardized as ECMAScript, and now run everywhere from servers to build tools despite quirks it can never remove.

Web Platform10 connections

requestAnimationFrame

The browser callback that runs once per display frame just before paint, which is the right clock for visual work and stops entirely when nothing is being displayed.

Web Platform12 connections

Background Tab Throttling

Browsers deliberately slow, freeze, or discard pages that are not visible, so code that assumes it keeps running in the background will be wrong.

Web Platform13 connections

Thread Pool

A fixed set of threads kept alive to run many short tasks from a queue, avoiding the cost of creating a thread per task and capping concurrency, with sizing rules and failure modes that shape how servers and runtimes behave under load.

Systems & Tooling10 connections

Web Server

The program that listens for HTTP requests and answers them with files or generated pages, from the first one at CERN in 1990 through Apache's long dominance to event-driven servers built to hold many thousands of connections at once.

Web Platform10 connections

Linked from17