谁能解释为什么最后设置超时打印?

Can anyone explain why set timeout print in last?

预期的输出,虽然我 运行 它在 repl 上,与此处描述的完全不同 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'));

它给出输出 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

我理解的预期结果

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


我正在这里阅读 https://jsblog.insiderattack.net/promises-next-ticks-and-immediates-nodejs-event-loop-part-3-9226cbe7a6aa

因为setTimeout()是异步的。你把它放在代码的什么地方并不重要。

检查此 link 以供参考 -

你的问题是IO引起的

如果你 运行 在本地 Node.js 安装你的代码输出就没问题。

在repl.it中不是因为有IO操作切换immediate和timeouts的执行。

您可以在本地复制:

'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'));

打印出来:

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

唯一的真实来源是this Node.js guide: 我认为 repl.it 对您在 GUI 中编写的代码应用了一些逻辑,因此 setImmediate 进入 poll 阶段而不是 check 所以它被切换了。