0

I'm trying to understand Nodejs's official explanation about Event Loop. They have explained an example for Timers phase, but I'm unable to match it with their explanation about setTimeout and setImmediate.

Could you please explain, in detail, all the steps/processes/checks that Event Loop does for following pieces of codes?

Code 1:

setTimeout(() => {
  console.log('timeout');
}, 0);

setImmediate(() => {
  console.log('immediate');
});

Code 2:

const fs = require('fs');
fs.readFile(__filename, () => {
  setTimeout(() => {
    console.log('timeout');
  }, 0);
  setImmediate(() => {
    console.log('immediate');
  });
});

PS1: In comments, Mark has asked me to explain, which part is confusing. Here you go:

Following is summary of their explanation for their first example: "When the event loop enters the poll phase, it has an empty queue (fs.readFile() has not completed), so it will wait for the number of ms remaining until the soonest timer's threshold is reached...then wraps back to timers phase and runs its callbacks"

So, what I understand is that, accordingly for code 1, it should be as following: Poll phase has empty queue, and time threshold is reached. So, firstly setTimeout should run. Then going to check phase and should setImmediate runs. But it doesn't behave like this. why?

Mahdi
  • 1,089
  • 1
  • 14
  • 27
  • 2
    When I run these snippets they behave exactly as the documented. Maybe you could mention the behavior that you are seeing that you don't understand. – Mark Dec 24 '17 at 00:02
  • @Mark_M, this is summary of their explanation, for their example, which is in timers phase: "When the event loop enters the poll phase, it has an empty queue (fs.readFile() has not completed), so it will wait for the number of ms remaining until the soonest timer's threshold is reached...then wrap back to timers phase and runs its callbacks" So, accordingly for code 1, it should be as following: Poll phase has empty queue, and time threshold is reached. So, firstly setTimeout should run. Then going to check phase and setImmediate runs. But it doesnt behave like this. why? – Mahdi Dec 24 '17 at 00:31
  • See the very end of the answer this is marked a dup of [Why setImmediate() execute before fs.readFile() in Nodejs Event Loop's works?](https://stackoverflow.com/questions/47724811/why-setimmediate-execute-before-fs-readfile-in-nodejs-event-loops-works) for an explanation of why code 2 runs differently than code 1. – jfriend00 Dec 24 '17 at 01:00
  • @jfriend00 I searched but couldnt find something similar. thanks for pointing that out. I'm currently reading your answer over there, and will probably shoot you message over there. – Mahdi Dec 24 '17 at 01:05
  • @Ali - If you read the last part of that answer, it shows your code snippet and explains it. – jfriend00 Dec 25 '17 at 15:40
  • @jfriend00 thank you. You explained it well over there, but your logic doesn't comply with some scenarios. Could you please take a look at following question? https://stackoverflow.com/questions/47970806/why-node-js-setimmediate-executes-after-i-o-callbacks/47970967#47970967 – Mahdi Dec 25 '17 at 18:27

0 Answers0