JAVASCRIPT GUIDE
Microtasks vs Macrotasks in JavaScript
By the HireReadyAI team · 7 min read
Ordering rules for promises and timers, and traps that show up in interview snippets.
Definitions
Macrotasks (often just called tasks) schedule work like timers. Microtasks schedule Promise continuations and related queueMicrotask callbacks. After each macrotask, the engine drains microtasks before rendering or the next macrotask.
Ordering rules
A resolved Promise’s then runs before a setTimeout(0) scheduled in the same turn. Nested microtasks keep draining until the queue is empty — which is why a runaway microtask loop can starve rendering and timers.
Traps
Assuming setTimeout(fn, 0) means “immediate,” or that await “blocks the browser,” are common mistakes. await splits your function across microtasks and later tasks when waiting on real async I/O.