hui
hui

Reputation: 601

what's difference between nodejs with browser about event-loop in below code?

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 runtime output: d a c f h b g e

Google Browser runtime output: d a c f h g b e

why output different result?

Upvotes: 4

Views: 289

Answers (1)

Artur P.
Artur P.

Reputation: 896

I think because NodeJS has own implementation of timers:

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.

About event-loop in node: https://nodejs.org/en/docs/guides/event-loop-timers-and-nexttick/

Upvotes: 2

Related Questions