What is the Event Loop?
The Event Loop is the specialized mechanism that allows JavaScript to perform non-blocking asynchronous operations, despite being a single-threaded language. It determines how and when code should execute, providing order to your asynchronous tasks.
javascript
How It Works: The Components
- The Call Stack: Where your synchronous code is executed (in the order it appears).
- Web APIs (Browser): Background tasks (like
setTimeout,fetch, or event listeners) are handled by the browser context. - The Task Queue (Macro-Task Queue): Where callback results from
setTimeout,setInterval, and I/O are put when they're finished. - The Micro-Task Queue: Where
thenblocks from Promises are put. This queue has priority!
The Execution Order
Wait! The event loop follows a specific order of operations:
- Execute everything in the Call Stack (Synchronous code).
- Execute everything in the Micro-Task Queue (Promises).
- Execute the FIRST item in the Task Queue (Macrotasks).
- Repeat.
Note: Microtasks (Promises) always run before Macrotasks (likesetTimeout), even if the timeout was set before the promise!
Visualizing the Loop
text
Blocking the Event Loop (Avoid This!)
If you write a CPU-heavy synchronous loop (e.g.,
while(true) {}), it will block the Call Stack and take over the Event Loop. This makes your browser freeze and entirely unresponsive!javascript
Summary
| Framework | Mechanism | Priority |
|---|---|---|
| Stack | Synchronous | 1st (The engine!) |
| Microtask | Promises, queueMicrotask | 2nd (Highest async priority!) |
| Macrotask | setTimeout, setInterval, I/O | 3rd (Lower async priority!) |
| Best For | Understanding async timing, performance | |
| V8 | The engine behind the Event Loop inside Chrome/Node | |
| Task | A generic unit of asynchronous work | |
| Tick | A single full rotation of the event loop! | |
| Node.js | Uses the libuv library for its event loop (Multi-threaded I/O!) | |
| UI Rendering | Only happens after the microtask queue is empty | |
| Blocking | Never run heavy loops in the main thread! | |
| Worker | Use Web Workers for heavy CPU-bound tasks! | |
| Performance | Event loop ensures smooth animations and interactions | |
| Priority | Always remember: Promises BEFORE Timeouts! |