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)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay