The event loop is a runtime mechanism that repeatedly checks for completed asynchronous work and runs the matching callbacks one at a time. It lets a single thread handle many concurrent operations, such as network requests and timers, without blocking.
Why it matters
JavaScript in browsers and Node.js, along with Python's asyncio, relies on an event loop. Slow input and output happens outside the main thread, and when it finishes its callback is queued. The loop runs each callback to completion before taking the next. This is efficient for I/O-heavy servers, but any long synchronous task blocks everything else on the loop, freezing a page or delaying every request on a server.
Example
console.log("A");
setTimeout(() => console.log("B"), 0);
Promise.resolve().then(() => console.log("C"));
console.log("D");
// A, D, C, B
Synchronous code runs first. Promise callbacks (microtasks) run before timer callbacks, so C comes before B even with a zero delay.
Common mistakes
- Running heavy computation on the main loop instead of a worker or separate process.
- Assuming
setTimeout(fn, 0)runs immediately. - Believing a single-threaded loop is the same as having no concurrency.
Related
Get notified when this term updates
Plus occasional practical tips on uptime and monitoring, nothing else. Unsubscribe anytime.
This page is general information written by our editorial team and updated when documentation changes. Servers and providers differ, so test changes safely first. See our editorial policy and disclaimer.