1

The expected output, while i run it on repl, is quite different as described here https://jsblog.insiderattack.net/promises-next-ticks-and-immediates-nodejs-event-loop-part-3-9226cbe7a6aa

Promise.resolve().then(() => console.log('promise1 resolved'));
Promise.resolve().then(() => console.log('promise2 resolved'));
Promise.resolve().then(() => {
    console.log('promise3 resolved');
    process.nextTick(() => console.log('next tick inside promise resolve handler'));
});

Promise.resolve().then(() => console.log('promise4 resolved'));
Promise.resolve().then(() => console.log('promise5 resolved'));

setImmediate(() => console.log('set immediate1'));
setImmediate(() => console.log('set immediate2'));


process.nextTick(() => console.log('next tick1'));
process.nextTick(() => console.log('next tick2'));
process.nextTick(() => console.log('next tick3'));



setTimeout(() => console.log('set timeout'), 0);
setImmediate(() => console.log('set immediate3'));
setImmediate(() => console.log('set immediate4'));

It gives output repl is here https://repl.it/@anupambharti/SomeImmenseProfile

next tick1
next tick2
next tick3
promise1 resolved
promise2 resolved
promise3 resolved
promise4 resolved
promise5 resolved
next tick inside promise resolve handler
set immediate1
set immediate2
set immediate3
set immediate4
set timeout

The expected out with my understanding

next tick1
next tick2
next tick3
promise1 resolved
promise2 resolved
promise3 resolved
promise4 resolved
promise5 resolved
next tick inside promise resolve handler
set timeout
set immediate1
set immediate2
set immediate3
set immediate4


I was reading it here https://jsblog.insiderattack.net/promises-next-ticks-and-immediates-nodejs-event-loop-part-3-9226cbe7a6aa

  • 3
    Well, MDN says "This feature is non-standard and is not on a standards track. Do not use it on production sites facing the Web: it will not work for every user. There may also be large incompatibilities between implementations and the behavior may change in the future". So "doesn't works as expected" seems to be `setImmediate`'s built in feature. – mbojko Apr 15 '19 at 11:55
  • Thanks, I want to understand by this how code executes in NodeJs environment, which will get first to execute and how it passes through event-loop. – Anupam Bharti Apr 15 '19 at 12:00
  • @AnupamBharti - interesting, I got response you would expect on my localhost with 10.15.0 node (windows). I got the second one (same as you) on repl.it which is also 10.15.x version. – libik Apr 15 '19 at 12:08
  • @libik on my localhost and repl all is same, localhost is 8.10.0 ( Linux ), it gives me a headache – Anupam Bharti Apr 15 '19 at 12:13
  • @AnupamBharti - then it looks like the windows and linux distribution is maybe a bit different. – libik Apr 15 '19 at 12:23
  • @libik it may be, but which one makes sense? I want to understand a clear picture of how it gets run through event-loop phases. – Anupam Bharti Apr 15 '19 at 12:36
  • @AnupamBharti - as you can see, both "makes sense" as both can happen. The answer is - when programming, the code should not rely on this. Its good to understand technologies you use more deeply, but in this case I think its by far enough to know about event-loop and one-thread and asynchronous behaviour. You can take the exact details as black box. – libik Apr 15 '19 at 12:43

2 Answers2

0

Your trouble is caused by IO.

If you run in a local Node.js installation the output of your code it is ok.

In repl.it is not because there is a IO operation that switch the execution of immediate and timeouts.

You can reproduce it locally:

'use strict';

const fs = require('fs')

Promise.resolve().then(() => console.log('promise1 resolved'));
Promise.resolve().then(() => console.log('promise2 resolved'));
Promise.resolve().then(() => {
    console.log('promise3 resolved');
    process.nextTick(() => console.log('next tick inside promise resolve handler'));
});

Promise.resolve().then(() => console.log('promise4 resolved'));
Promise.resolve().then(() => console.log('promise5 resolved'));

setImmediate(() => console.log('set immediate1'));
setImmediate(() => console.log('set immediate2'));


process.nextTick(() => console.log('next tick1'));
process.nextTick(() => console.log('next tick2'));
process.nextTick(() => console.log('next tick3'));

fs.readFile(__filename, () => {
    setTimeout(() => console.log('set timeout +2'), 0);
    setImmediate(() => console.log('set immediate +5'));
    setImmediate(() => console.log('set immediate +6'));
});

setTimeout(() => console.log('set timeout'), 0);
setImmediate(() => console.log('set immediate3'));
setImmediate(() => console.log('set immediate4'));

Print out:

next tick1
next tick2
next tick3
promise1 resolved
promise2 resolved
promise3 resolved
promise4 resolved
promise5 resolved
next tick inside promise resolve handler
set timeout
set immediate1
set immediate2
set immediate3
set immediate4
set immediate +5
set immediate +6
set timeout +2

The unique source of truth is this Node.js guide: I think that repl.it applies some logic on the code you write in the GUI, so the setImmediate go to the poll phase instead of the check so it is switched.

Manuel Spigolon
  • 11,003
  • 5
  • 50
  • 73
  • It also gives you the same output as in repl.it, if you try to run code directly in console on local, can we say it is not repl.it GUI it is node console which put event-loop in poll phase, if yes then it makes sense :) – Anupam Bharti Apr 15 '19 at 14:11
-1

It is because setTimeout() is asynchronous. It doesn't matter where you put it in the code.

Check this link for reference -

What is setTimeout doing when set to 0 milliseconds?

  • I understand the nature of setTimeout(), but shouldn't it be executed based on the phases in event-loop? look at the comment @libik getting both of the output. – Anupam Bharti Apr 15 '19 at 12:21