The callback to execute.
Optionaloptions: MicroTaskOptionsOptional per-call fallback options when queueMicrotask is unavailable.
A handler that can be used to cancel or refresh the scheduled callback.
let order: string[] = [];
order.push("sync");
const handler = scheduleMicrotask(() => {
order.push("microtask");
});
scheduleTimeout(() => {
order.push("timeout");
}, 0);
// order becomes ["sync", "microtask", "timeout"]
handler.enabled; // true until the microtask executes
Schedules a callback to run in the microtask queue.
It uses the native queueMicrotask when available, otherwise falls back to
Promise.resolve().then(...), and if Promise is unavailable it falls back to
scheduleTimeout(..., 0).
Unlike standard microtasks, this helper returns a cancellable ITimerHandler
so scheduled callbacks can be canceled before execution.
This provides consistent microtask scheduling behavior across all supported runtimes,
including Node.js, browsers, and web workers.
The callback to execute.
Optional callback arguments to pass when the callback executes.
Optionaloptions: MicroTaskOptionsOptional per-call fallback options when queueMicrotask is unavailable.
A handler that can be used to cancel or refresh the scheduled callback.
let order: string[] = [];
order.push("sync");
const handler = scheduleMicrotask(() => {
order.push("microtask");
});
scheduleTimeout(() => {
order.push("timeout");
}, 0);
// order becomes ["sync", "microtask", "timeout"]
handler.enabled; // true until the microtask executes
Schedules a callback to run in the microtask queue.
It uses the native
queueMicrotaskwhen available, otherwise falls back toPromise.resolve().then(...), and ifPromiseis unavailable it falls back toscheduleTimeout(..., 0). Unlike standard microtasks, this helper returns a cancellableITimerHandlerso scheduled callbacks can be canceled before execution. This provides consistent microtask scheduling behavior across all supported runtimes, including Node.js, browsers, and web workers.