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

时间:2019-04-15 11:49:34

标签: javascript node.js event-loop

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

它给出的输出表示在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阅读

2 个答案:

答案 0 :(得分:0)

您的麻烦是由IO引起的。

如果在本地Node.js安装中运行,则代码输出正常。

这并不是因为有IO操作可以切换立即执行和超时执行。

您可以在本地复制它:

'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阶段,因此被切换了。

答案 1 :(得分:-1)

这是因为setTimeout()是异步的。放在代码中的位置都没关系。

检查此链接以供参考-

What is setTimeout doing when set to 0 milliseconds?