queueMicrotask runs before the next paint, setTimeout after it
They are not interchangeable. A microtask runs as soon as the current task finishes, before the browser paints. A setTimeout(fn, 0) runs on the next task, after any pending render.
queueMicrotask(() => flushBatch()); // still this frame
setTimeout(() => measureLayout(), 0); // after layout and paintIf you want the DOM to have updated first, it is the timeout. If you want to batch work that must happen before the user sees anything, the microtask.
javascript