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
- callback function — A function you hand to other code so that it can call it later, for example when a timer fires or an event happens.
- linked list — A sequence made of nodes scattered in memory, each holding a value and a pointer to the next node.
- priority queue — A collection where each item has a priority and the item removed next is always the one with the highest priority, not the oldest.
- call stack — The region of memory where a running program keeps one frame for each function call that has started but not yet returned.
- macrotask — An ordinary task scheduled on the event loop, such as a timer or I/O callback, as opposed to a microtask.
- microtask — A small job, such as a promise callback, that runs as soon as the current piece of JavaScript finishes, before the event loop handles anything else.
- rendering opportunity — A point in the event loop, between tasks, when the browser may run animation callbacks, recalculate style and layout, and paint a new frame.
- main thread — The first thread a program starts with; in a browser page it also runs JavaScript and handles input, layout and painting.
- event loop phase — One of the six stages the Node.js event loop visits in order on every turn: timers, pending callbacks, idle/prepare, poll, check and close callbacks.
- callback — A function you pass to other code so it can call you back later, for example when an event happens or a timer fires.
- libuv — The C library inside Node.js that runs the event loop, performs asynchronous I/O and manages the thread pool.
Taught in
- Linked Lists: Nodes, Pointers And Sentinels — Algorithms & Data Structures for Working Engineers
- The Browser Event Loop: Call Stack, Microtasks And Macrotasks — Frontend Architecture & Web Platform
- requestAnimationFrame vs requestIdleCallback: High-FPS Work — Frontend Architecture & Web Platform
- V8 Engine & JIT Pipeline: Bytecode & Turbofan — Node.js Internals & Backend Services
- libuv Architecture: Event Demultiplexer & Thread Pool — Node.js Internals & Backend Services
- Event Loop Lifecycle: The Six Phases & libuv uv_run — Node.js Internals & Backend Services
- Microtask Queues: process.nextTick vs Promise Timing — Node.js Internals & Backend Services
- Timers Phase: setTimeout, Min-Heap Scheduling & Drift — Node.js Internals & Backend Services
- Poll & Check Phases: I/O Callbacks & setImmediate — Node.js Internals & Backend Services
- Close Phase & Signal Dispatch: Sockets & Teardown — Node.js Internals & Backend Services
- Worker Threads: worker_threads & SharedArrayBuffer — Node.js Internals & Backend Services