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.
यह पेज सामान्य जानकारी है, जिसे हमारी संपादकीय टीम ने लिखा है और दस्तावेज़ीकरण बदलने पर अपडेट किया जाता है। सर्वर और प्रदाता अलग-अलग होते हैं, इसलिए बदलाव पहले सुरक्षित तरीके से जाँचें। देखें: संपादकीय नीति और अस्वीकरण.