Task Queue and Job Queue - Deep dive into Javascript Event Loop Model

Co-Founder, @CreoWis | Teacher, @tapaScript | Founder, @ReactPlay | YouTuber | Writer | Human
Search for a command to run...

Co-Founder, @CreoWis | Teacher, @tapaScript | Founder, @ReactPlay | YouTuber | Writer | Human
it is very very useful thanks alot
MacroTasks are called, Tasks and MicroTasks are called Jobs.
This ruined me for about an hour until I realized the comma was in the wrong place
Thanks for reporting this. Corrected.
Can you please explain this me why the event loop prioritize cartoon's setTimeouts than the then's setTimeout? Ok, I did some changes here
const tom = () => console.log('Tom');
const jerry = () => console.log('Jerry');
const doggy = () => console.log('Doggy');
const cartoon = () => {
console.log('Cartoon');
setTimeout(tom, 0);
setTimeout(doggy, 0);
new Promise((resolve, reject) =>{
resolve('I am a Promise, right after tom and doggy! Really?');
setTimeout(()=>{console.log('inner timeout')},0)
}
).then(resolve => console.log(resolve));
new Promise((resolve, reject) =>
resolve('I am a Promise after Promise!')
).then(resolve => console.log(resolve));
jerry();
}
cartoon();
"Cartoon"
"Jerry"
"I am a Promise, right after tom and doggy! Really?"
"I am a Promise after Promise!"
"Tom"
"Doggy"
"inner timeout"
I think it doesn't matter if the setTimeout is inside then, because it will still get added to the TASK queue and since all the timers are 0, they will get executed in order they got en-queued.
tom -> doggy -> inner timeout
Jalaj Gupta , That's absoloutely correct.
Thank you very much. Very detailed and mind blowing explanation. Understood. )
Thank you! Glad it was helpful 😍
Thanks for great Article.
Cartoon
Jerry
//These micro task has precedence over macro tasks
I am a Promise, right after tom and doggy! Really?
I am a Promise after Promise!
//Finally macro tasks
Doggy
Tom
So the expected output would be:
You were very close. Well tried and Thanks for trying. Thumbs up for it!
Tapas Adhikary I didn't understood the 11th point in the above explanation. setTimeout(tom, 50); setTimeout(doggy, 30); The task queue is filled this way: [tom(), doggy()] So why isn't the output this because task queue is a task so FIFO no matter the timeout duration: Cartoon Jerry I am a Promise, right after tom and doggy! Really? I am a Promise after Promise! Tom Doggy
Vedamruta Udavant No for setTimeout, setInterval kind of web APIs it will be filled based on the delayed time. That's the catch you need to keep in mind. So it will [doggy(), tom()] this way.
Tapas Adhikary Got it, thank you 😄
Traditional SSR is great, but it has a massive buffering problem. Let's look into the SSR Streaming and learn how it could be a game changer.

Learn how to build maintainable, reusable forms in React using design patterns like custom hooks, compound components, and context for scalable CRUD

Learn how JavaScript variables work, including let vs const, primitive and reference types, pass by value, and how JavaScript stores data in memory.

A beginner-friendly introduction to JavaScript covering what it is, how it works, and how to set up your JavaScript environment

Learn how the promises are handled internally using event loop. This concept is essentials to debug and work with async programming.

GreenRoots Blog - A Blog by Tapas Adhikary
186 posts
Educator | YouTuber | Building ReactPlay | Let's Connect
There is a famous saying,
Keep every promise you make and only make promises you can keep.
In my last post, I also made a promise that I shall write about the concept of Task and Job Queues. Here is the post on it. Interestingly, this post is mostly about How to keep a Promise and execute it.
Before we move further, I would like to clarify that I will not explain Promises as a concept here. There are plenty of good reads on it. Here is my personal favorite. However, this particular post is about understanding what goes under the hood when a promise gets executed? Along with it, we will also learn the difference between Task Queue and Job Queue.
To recall, some of it already explained here that there is Task Queue in the Event Loop Model. There is a Job Queue as well. Here are a few essential points to note:
Tasks, and MicroTasks are called Jobs.For example, Promises are in Job Queue, and the functions for setTimeOut are in TaskQueue.
With the explanation above, let us re-visit the Event Loop Model once more than last time.

The obvious question would be, how does the event loop decide which queue to de-queue from and push to Call Stack when the Stack is empty? The answer depends on this logic(or, set of rules):
Overall, the Event Loop got one more item to consider in its orchestration of Code Execution. Let us understand the above logic with a Code execution flow.
const tom = () => console.log('Tom');
const jerry = () => console.log('Jerry');
const cartoon = () => {
console.log('Cartoon');
setTimeout(tom, 5000);
new Promise((resolve, reject) =>
resolve('should it be right after Tom, before Jerry?')
).then(resolve => console.log(resolve))
jerry();
}
cartoon();
So the expected output is,
Cartoon
Jerry
should it be right after Tom, before Jerry?
Tom
Let me explain Why?
cartoon gets into the Call Stack.setTimeOut web API goes outside of the Call Stack in the following execution line, and the associated function tom gets placed into TaskQueue.jerry is pushed into the Stack and executed.tom gets to the Call Stack from TaskQueue and gets executed.That's all about it. I hope you got the core concept. Now, Here is a puzzle for you. Let me know what the expected output of this code execution is? Feel free to post a comment with your answer.
const tom = () => console.log('Tom');
const jerry = () => console.log('Jerry');
const doggy = () => console.log('Doggy');
const cartoon = () => {
console.log('Cartoon');
setTimeout(tom, 50);
setTimeout(doggy, 30);
new Promise((resolve, reject) =>
resolve('I am a Promise, right after tom and doggy! Really?')
).then(resolve => console.log(resolve));
new Promise((resolve, reject) =>
resolve('I am a Promise after Promise!')
).then(resolve => console.log(resolve));
jerry();
}
cartoon();
Tips: Don't be upset if you hear any of your friends talking about another queue called Message Queue. They are just referring to Task Queue just by another name.
I hope you liked the post. Cheers!