My FeedDiscussionsHeadless CMS
New
Sign in
Log inSign up
Learn more about Hashnode Headless CMSHashnode Headless CMS
Collaborate seamlessly with Hashnode Headless CMS for Enterprise.
Upgrade ✨Learn more
Tapas Adhikary

45 likes

·

13.8K reads

14 comments

Izzatullo Kanoatov
Izzatullo Kanoatov
Jan 31, 2021

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

2
·
·1 reply
Tapas Adhikary
Tapas Adhikary
Author
·Jan 31, 2021

Thank you! Glad it was helpful 😍

·
Adrian Belen
Adrian Belen
Jun 15, 2021

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
·
·2 replies
Jalaj Gupta
Jalaj Gupta
Jun 27, 2021

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
·
Tapas Adhikary
Tapas Adhikary
Author
·Aug 25, 2021

Jalaj Gupta , That's absoloutely correct.

·
Thamaraiselvam
Thamaraiselvam
Apr 15, 2019

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
·
·5 replies
Tapas Adhikary
Tapas Adhikary
Author
·Apr 15, 2019
  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
·
Thamaraiselvam
Thamaraiselvam
Apr 15, 2019

oops yes your are correct

2
·
Vedamruta Udavant
Vedamruta Udavant
Jul 27, 2023

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
·
Tapas Adhikary
Tapas Adhikary
Author
·Jul 28, 2023

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
·
Vedamruta Udavant
Vedamruta Udavant
Jul 28, 2023

Tapas Adhikary Got it, thank you 😄

·
xy Zing
xy Zing
Dec 24, 2021

Nice post for beginner. Thank you!

1
·
Brian Coverstone
Brian Coverstone
Jan 7, 2022

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
·
·1 reply
Tapas Adhikary
Tapas Adhikary
Author
·Jan 10, 2022

Thanks for reporting this. Corrected.

·