SyntaxStudy
Sign Up
JavaScript Microtasks and Event Loop
JavaScript Advanced 5 min read

Microtasks and Event Loop

Microtask Queue

Promise callbacks (.then, .catch, async continuations) run in the microtask queue — before the next macrotask (setTimeout, setInterval) but after the current synchronous code.

Example
console.log("1 sync");
setTimeout(() => console.log("3 macrotask"), 0);
Promise.resolve().then(() => console.log("2 microtask"));
console.log("1b sync");
// Output: "1 sync", "1b sync", "2 microtask", "3 macrotask"
Pro Tip

Microtasks flush completely before any macrotask runs — starving with microtasks blocks rendering.