For urgent work that must run before the next event or paint. queueMicrotask adds the callback to the microtask queue, which the event loop drains before rendering and macrotasks.
setTimeout vs requestAnimationFrame vs queueMicrotask in JavaScript
When should you use queueMicrotask?
The answer
More FAQs in setTimeout vs requestAnimationFrame vs queueMicrotask in JavaScript
setTimeout is a macrotask (lowest priority, runs after rendering). requestAnimationFrame runs before the next paint (for animations). queueMicrotask is a microtask (highest priority, runs before rendering and macrotasks).
For animations and visual DOM updates. requestAnimationFrame syncs with the browser's refresh rate (usually 60fps), runs before the next paint, and produces smoother animations than setTimeout.
Sync code runs first. Then microtasks (queueMicrotask, Promise.then). Then requestAnimationFrame (before the next paint). Then setTimeout (macrotask, after rendering).
Still have questions?
Browse all our FAQs or reach out to our support team
Want to upskill yourself?
Your learning library
Small steps. Big possibilities.
Find your next course. Build something great.