DEV Community

Gajender Tyagi
Gajender Tyagi

Posted on

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)