Open this example in the interactive library
Source code
const steps = [];
queueMicrotask(() => steps.push("microtask"));
steps.push("synchronous");
await Promise.resolve();
console.log(steps);
Sample output
[ 'synchronous', 'microtask' ]

Schedule a microtask after synchronous work in JavaScript.
Open this example in the interactive library
const steps = [];
queueMicrotask(() => steps.push("microtask"));
steps.push("synchronous");
await Promise.resolve();
console.log(steps);
[ 'synchronous', 'microtask' ]