logo

Event Loop Deep Dive

A deeper explanation of the event loop, microtasks, macrotasks, and practical implications for async code.

Short explanation

This deep dive explains task ordering, microtask checkpoints, and how Promises and other async APIs interleave with rendering and I/O.

Example

console.log('A');
setTimeout(() => console.log('B'), 0);
Promise.resolve().then(() => console.log('C'));
console.log('D');
// Expected: A, D, C, B

How JS handles it internally

After the call stack empties, the runtime processes the microtask queue fully before continuing with the next macrotask. This behavior affects update sequencing and perceived performance in UI apps.

FAQ

Q: How do microtasks affect rendering?

A: A long-running series of microtasks can delay rendering because the microtask queue is drained before the rendering step.