Skip to main content

Command Palette

Search for a command to run...

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

Updated
4 min read
Task Queue and Job Queue - Deep dive into Javascript Event Loop Model
T

Co-Founder, @CreoWis | Teacher, @tapaScript | Founder, @ReactPlay | YouTuber | Writer | Human

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:

  • Not all the tasks are created of the same priority. There are macrotasks and microtasks.
  • MacroTasks are called Tasks, and MicroTasks are called Jobs.
  • Examples of macrotasks are setTimeout, setInterval, setImmediate, I/O tasks, etc.
  • Examples of Microtasks are, Promises, processes.nextTick, etc.
  • The Queue in Event Loop Model holds the Tasks(or the MacroTasks) called, TaskQueue.
  • The Queue in Event Loop Model holds the Jobs(or the MicroTasks) called, JobQueue.
  • 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.

eventloopmodel.png

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):

  • For each loop of the 'event loop', one macrotask(Task) is completed out of the macrotask(Task) queue.
  • Once that task is complete, the event loop visits the microtask(Job) queue. The entire microtask(Job) queue is completed before the 'event loop' looks into the next thing.
  • At any point in time, if both the queues got entries, JobQueue gets higher precedence than TaskQueue.

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?

  • The function cartoon gets into the Call Stack.
  • It executes the console log of the text Cartoon. The setTimeOut web API goes outside of the Call Stack in the following execution line, and the associated function tom gets placed into TaskQueue.
  • In the following line of execution, we encounter a Promise. A callback of a promise gets a place in JobQueue. Hence the console log function execution on the promise goes to JobQueue.
  • In the following execution line, the function jerry is pushed into the Stack and executed.
  • Now the fun begins. We have one entry in TaskQueue and one in JobQueue. The Event Loop Model prioritizes all the Jobs in JobQueue over anything in TaskQueue. Hence the callback of the promise get to the Call Stack first, gets executed, and then the function 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!

W

it is very very useful thanks alot

B

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

1
T

Thanks for reporting this. Corrected.

X
xy Zing4y ago

Nice post for beginner. Thank you!

1
A

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"

2
J

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

1
T

Jalaj Gupta , That's absoloutely correct.

I

Thank you very much. Very detailed and mind blowing explanation. Understood. )

2
T

Thank you! Glad it was helpful 😍

T

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

1
T
  1. First the function cartoon gets into the Stack.
  2. Next in call stack, Console.log('Cartoon') gets executed
  3. Next in call stack, setTimeOut -> tom is web api, so tom goes to TaskQueue but has not been executed yet.
  4. Next in call stack, setTimeOut -> doggy is web api, so doggy goes to TaskQueue but has not been executed yet.
  5. Next promise in call stack, the then() callback goes to job queue but has not been executed yet.
  6. Next another promise in call stack, the then() callback goes to job queue but has not been executed yet.
  7. Next in call stack, is jerry().
  8. Next in call stack, console.log('Jerry').
  9. At this point, nothing in Call Stack. The event loop looks into both the Queues. Gives priority to Job Queue over Tasks Queue.
  10. Execute both the promises callbacks from Job Queue by pushing those to Call Stack one by one.
  11. Now both Call Stack and Job Queue are empty. The Event Loop executes both the tasks from Task Queue.

So the expected output would be:

  • Cartoon
  • Jerry
  • I am a Promise, right after tom and doggy! Really?
  • I am a Promise after Promise!
  • Doggy
  • Tom

You were very close. Well tried and Thanks for trying. Thumbs up for it!

Thamaraiselvam

1
T

oops yes your are correct

2
V

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

1
T

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.

1
V

Tapas Adhikary Got it, thank you 😄