JavaScript
Cap nhat 17 thg 1, 2026JavaScript Event Loop Explained
How the event loop works: call stack, task queue, microtask queue, and rendering pipeline.
javascript
event-loop
async
The Call Stack
JavaScript is single-threaded. The call stack tracks function execution. LIFO order.
Web APIs
Browser provides async APIs (setTimeout, fetch, DOM events). These run outside the call stack.
Task Queue (Macrotasks)
Callbacks from setTimeout, setInterval, I/O. Processed one at a time between renders.
Microtask Queue
Promise callbacks (.then, .catch, .finally) and queueMicrotask(). Processed ALL before next macrotask.
Execution Order
1. Execute synchronous code on the call stack
2. Drain the microtask queue
3. Render if needed
4. Process one macrotask
5. Repeat from step 2