Normalize synchronous and asynchronous paths
A cached result must still be delivered asynchronously so callers observe consistent timing.
Node.js · Function
Queues a callback as a V8 microtask for asynchronous execution after the current code completes. Node processes process.nextTick() callbacks before this microtask queue within an event-loop turn.
queueMicrotask(callback) schedules the supplied function in V8's microtask queue. Node.js processes that queue after the current execution completes; within each event-loop turn, the process.nextTick() queue runs before it. A callback exception is reported through the process uncaughtException event.
queueMicrotask(callback)| Name | Type | Description |
|---|---|---|
callback | Function | Function to be queued for invocation as a microtask. |
void
process's uncaughtException event rather than being returned.process.nextTick(); Node drains the next-tick queue first in each event-loop turn.queueMicrotask(() => {
console.log('microtask');
});
console.log('synchronous');The synchronous message prints first; the queued callback runs afterward in the microtask phase.
A cached result must still be delivered asynchronously so callers observe consistent timing.
Small follow-up work should run before timers and ordinary I/O callbacks.
An API needs Promise-like microtask scheduling without creating a Promise.
queueMicrotask() do?It accepts one callback function and returns no value. The callback is placed on V8's microtask queue and invoked asynchronously after the current execution completes.
Follow-up: How does it compare with process.nextTick()? Node processes the next-tick queue before the microtask queue during each event-loop turn.
queueMicrotask()?The callback is executed as a microtask rather than as a timer or ordinary I/O callback. This makes it useful for deferring work while keeping it ahead of later event-loop phases.
Follow-up: How can a callback failure surface? A thrown exception causes the process uncaughtException event to be emitted.
Its parameter is callback, documented as a function to be queued. The function itself has no documented return value, so the API signature is queueMicrotask(callback) with a void result.
Follow-up: What runtime owns the queue? V8 manages the microtask queue, while Node.js manages the separate process.nextTick() queue.
process.nextTick()Promise