DEV Community

satyajit nayak
satyajit nayak

Posted on

understand priorities of queues in NodeJS

Guess the output!!

console.log('1');

setImmediate(() => {
  console.log('3');
});

setTimeout(() => {
  console.log('4');
}, 0);

process.nextTick(() => {
  console.log('2');
});

Enter fullscreen mode Exit fullscreen mode

OUTPUT:

1
2
4
3
Enter fullscreen mode Exit fullscreen mode

Guess How?? (Try to run the code here.)

Output depends on the priorities of different queues involves in the execution.

Priority of [ process.nextTick() > Timer Queue (setTimeout) > Check Queue (setImmediate) ]

read more about it

Top comments (0)