event loop

The central loop of an asynchronous program that waits for events, such as timers firing or data arriving, and runs the matching callbacks.

In more detail

Instead of blocking while it waits, the program registers callbacks and lets the loop run each one when its event arrives. Callbacks run one after another, so a slow callback delays every other one. Browsers, Node.js and Python's asyncio are all built around an event loop, although their loops are organised differently.

Examples

Python's asyncio is built around an event loop; timer queues use heaps or linked lists.
In the browser: JavaScript runs on one thread, so the event loop decides what runs next. Because rendering only happens between tasks, a long task or an endless chain of microtasks freezes the page.
In Node.js the loop is run by libuv's uv_run and visits six phases in order: timers, pending callbacks, idle/prepare, poll, check and close callbacks. It exits when no active handles or requests remain.

Related terms

Taught in

References