setImmediate(callback[, ...args])


安排在 I/O 事件回调之后立即执行 callback

【Schedules the "immediate" execution of the callback after I/O events' callbacks.】

当多次调用 setImmediate() 时,callback 函数会按照它们被创建的顺序排队等待执行。整个回调队列会在每次事件循环迭代中被处理。如果在正在执行的回调内部排队了一个立即定时器,该定时器要到下一次事件循环迭代时才会被触发。

【When multiple calls to setImmediate() are made, the callback functions are queued for execution in the order in which they are created. The entire callback queue is processed every event loop iteration. If an immediate timer is queued from inside an executing callback, that timer will not be triggered until the next event loop iteration.】

如果 callback 不是一个函数,将会抛出 TypeError

【If callback is not a function, a TypeError will be thrown.】

这种方法有一个适用于 Promise 的自定义变体,可以通过 timersPromises.setImmediate() 使用。

【This method has a custom variant for promises that is available using timersPromises.setImmediate().】