gpt4 book ai didi

javascript - 谁能解释一下为什么在最后设置超时打印?

转载 作者:太空宇宙 更新时间:2023-11-04 02:45:14 25 4
gpt4 key购买 nike

当我在 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

最佳答案

你的问题是IO引起的。

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

在repl.中并不是因为有一个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,因此它被切换。

关于javascript - 谁能解释一下为什么在最后设置超时打印?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55688555/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com