Microtask Queue
Explain microtasks vs macrotasks and why microtasks have higher priority.
Short explanation
Microtasks (Promise callbacks) run after the current call stack empties but before the next macrotask. They are often used for Promise resolution and DOM mutation observations.
Small snippet
Promise.resolve().then(() => console.log('microtask'));
setTimeout(() => console.log('macrotask'), 0);
console.log('sync');
// Expected: sync, microtask, macrotask
How JS handles it internally
After each macrotask, the runtime drains the microtask queue fully before continuing. This guarantees Promise callbacks run promptly but can starve rendering if abused.
FAQ
Q: Can microtasks delay rendering?
A: Yes β long microtask chains can block rendering because the microtask queue must be drained before rendering proceeds.