async function async1() {
console.log("a");
await async2();
console.log("b");
}
async function async2() {
console.log( 'c');
}
console.log("d");
setTimeout(function () {
console.log("e");
},0);
async1();
new Promise(function (resolve) {
console.log("f");
resolve();
}).then(function () {
console.log("g");
});
console.log('h');
nodejs 运行时输出:d a c f h b g e
Google 浏览器运行时输出:d a c f h g b e
为什么输出不同的结果?
最佳答案
我认为是因为 NodeJS 有自己的计时器实现:
https://nodejs.org/dist/latest-v9.x/docs/api/timers.html
The timer functions within Node.js implement a similar API as the timers API provided by Web Browsers but use a different internal implementation that is built around the Node.js Event Loop.
关于 Node 中的事件循环:https://nodejs.org/en/docs/guides/event-loop-timers-and-nexttick/
关于node.js - 关于下面代码中的事件循环,nodejs 和浏览器有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49546738/