setImmediate()
Schedules a callback to run after the current poll phase of the event loop.
Syntax
setImmediate(callback[, ...args]) Parameters
| Name | Type | Required | Description |
|---|---|---|---|
callback | function | Yes | The function to execute. |
args | ...any | No | Arguments passed to the callback. |
Returns
Immediate — A handle you can pass to clearImmediate().
Examples
console.log('start');
setImmediate(() => console.log('immediate'));
console.log('end');
Output
start
end
immediate
import { setImmediate as nextTickImmediate } from 'node:timers/promises';
console.log('before');
await nextTickImmediate();
console.log('after the I/O phase');
Output
before
after the I/O phase
Notes
Runs in the check phase, after I/O callbacks but before the next
timer cycle. Prefer it over `setTimeout(fn, 0)` to yield back to the
event loop. The `node:timers/promises` variant integrates with
async/await.