console.log('start'); process.nextTick(() => console.log('nextTick')); setImmediate(() => console.log('setImmediate')); console.log('end');
The synchronous logs 'start' and 'end' run first. Then process.nextTick callbacks run before setImmediate. So the order is 'start', 'end', 'nextTick', then 'setImmediate'.
console.log('start'); process.nextTick(() => console.log('tick1')); process.nextTick(() => console.log('tick2')); setImmediate(() => console.log('immediate')); console.log('end');
The synchronous logs 'start' and 'end' run first. Then both process.nextTick callbacks run in order, followed by the setImmediate callback.
setImmediate(() => console.log('setImmediate')); process.nextTick(() => console.log('nextTick')); // This code is inside a setTimeout callback setTimeout(() => { setImmediate(() => console.log('setImmediate inside timeout')); process.nextTick(() => console.log('nextTick inside timeout')); }, 0);
Callbacks scheduled inside setTimeout run in the timers phase. setImmediate callbacks scheduled inside setTimeout run in the check phase immediately after timers. process.nextTick callbacks scheduled outside setTimeout run before the event loop continues. So the setImmediate inside setTimeout runs after the timeout but before the nextTick outside it.
process.nextTick and setImmediate in Node.js?process.nextTick callbacks run immediately after the current JavaScript operation completes, before the event loop continues. setImmediate callbacks run on the next iteration of the event loop, after I/O events.
function test() {
setImmediate(() => console.log('immediate'));
process.nextTick(() => console.log('nextTick'));
}
test();setImmediate requires a function as its first argument. Passing a string causes a TypeError at runtime. Calling process.nextTick with a function that throws an error will throw inside the callback but does not cause an immediate runtime error on scheduling. Valid callbacks run without error.