javascript / intermediate
Snippet
Prioritizing Tasks with process.nextTick
process.nextTick() schedules a callback to run immediately after the current operation completes, before the event loop yields to macrosasks like setTimeout or I/O. It is used for microtask processing.
snippet.js
1
2
3
4
5
6
7
8
9
console.log('Script Start');setTimeout(() => console.log('Timeout (Macrotask)'), 0);process.nextTick(() => {console.log('Next Tick (Microtask)');});console.log('Script End');
nodejs
Breakdown
1
process.nextTick(() => {
Places the callback at the beginning of the next microtask queue.
2
setTimeout(..., 0);
Schedules a task in the macrotask queue, which runs after all pending microtasks.