代码如下所示,第一层的输出顺序1 7 6 8没有疑问,主要是两个setTimeout内部的执行顺序:
console.log('1');
setTimeout(function() {
console.log('2');
process.nextTick(function() {
console.log('3');
})
new Promise(function(resolve) {
console.log('4');
resolve();
}).then(function() {
console.log('5')
})
})
process.nextTick(function() {
console.log('6');
})
new Promise(function(resolve) {
console.log('7');
resolve();
}).then(function() {
console.log('8')
})
setTimeout(function() {
console.log('9');
process.nextTick(function() {
console.log('10');
})
new Promise(function(resolve) {
console.log('11');
resolve();
}).then(function() {
console.log('12')
})
})
现在的输出顺序有2种:
但是node.js v12.7.0中测试很多次貌似只有一种输出顺序
1
7
6
8
2
4
3
5
9
11
10
12
有没有大佬能解释下啊,网上的各种讲事件循环的都是看得云里雾里。
因为
node v11
有个pr
把nodejs
的行为改成和浏览器一致。原来的行为是,
micortask
执行完后,有timer
到期执行完所有到期timer
。现在的行为是,执行一个。