DEV Community

Gajender Tyagi
Gajender Tyagi

Posted on

3 1

Job Q V/S Callback Q

πŸ₯Š Who wins when we have tasks waiting to be implemented in Job queue as well as in Callback queue.

Hmmmm.... let's see who wins with the piece of code below. πŸ”ˆ

// setTimeout is a web API which waits for the time as given // in 2nd parameter and then moves the callback function in 
// call back queue   
setTimeout(() => console.log('1'), 0);
setTimeout(() => console.log('2'), 10);

// Promise is a class in JavaScript so the functionality is 
// native, to handle the functions executed we have a Job 
// queue
Promise.resolve('hi').then((data) => console.log('3'))

console.log('4');

Enter fullscreen mode Exit fullscreen mode

If you execute this code the logs will be

4
3
1
2
Enter fullscreen mode Exit fullscreen mode

This proves that JobQ has priority over callback queue if call stack is empty, Of course!.

Thoughts in comments

πŸ¦• ⌨️

Top comments (0)

The best way to debug slow web pages cover image

The best way to debug slow web pages

Tools like Page Speed Insights and Google Lighthouse are great for providing advice for front end performance issues. But what these tools can’t do, is evaluate performance across your entire stack of distributed services and applications.

Watch video

πŸ‘‹ Kindness is contagious

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

Okay