One thread doing many things
An event loop runtime executes your code on a single thread. That sounds limiting, yet servers built this way handle tens of thousands of connections. The trick is that the thread never waits idly. Instead of blocking on a slow operation, it hands the operation off and moves on to other ready work.
The loop itself
At its heart the loop is a simple cycle that repeats forever.
- Take the next ready callback from a queue.
- Run it to completion, with no interruption.
- Park any slow operations it starts, registering a callback for later.
- Repeat once the current callback returns.
Because each callback runs to completion, you never see another callback halfway modify your data mid function. This is the run to completion guarantee.
Why blocking is forbidden
If one callback runs a long loop or a synchronous file read, the whole loop freezes. Every other connection waits. The golden rule is to keep callbacks short and push slow work to the runtime, which watches for completion in the background.
Key idea
A single threaded event loop stays responsive by running short callbacks to completion and offloading slow operations, never blocking the one thread that drives everything.