Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The order of Task & MicroTask execution is inconsistent with browser #17181

Closed
dcy0701 opened this issue Nov 21, 2017 · 3 comments
Closed

The order of Task & MicroTask execution is inconsistent with browser #17181

dcy0701 opened this issue Nov 21, 2017 · 3 comments
Labels
promises Issues and PRs related to ECMAScript promises. question Issues that look for answers.

Comments

@dcy0701
Copy link

dcy0701 commented Nov 21, 2017

  • Version: 8.1.3
  • Platform: Mac OS
  • Subsystem: 10.12.4

Here are some code.

function func1(i){
      return new Promise((resolve,reject)=>{
          setTimeout(()=>{
               console.log("func1: ",i)
               resolve(true)
            },1000)
  })
}

async function func2(i){
        await func1(i)
        console.log("func2:",i)
}

function test(){
      for(var i=0; i<5; i++){
          func2(i)
         console.log("loop:",i)
      }
}

test();

When i execute it in Node, i got result like this:

loop: 0
loop: 1
loop: 2
loop: 3
loop: 4
func1:  0
func2: 0
func1:  1
func1:  2
func1:  3
func1:  4
func2: 1
func2: 2
func2: 3
func2: 4

But when i execute it in browser(MAC OS chrome 62.0.3202.94), i got result like this:

loop: 0
loop: 1
loop: 2
loop: 3
loop: 4
func1:  0
func2: 0
func1:  1
func2: 1
 func1:  2
func2: 2
func1:  3
func2: 3
func1:  4
func2: 4

I am puzzled by the result of this implementation.

@bnoordhuis bnoordhuis added promises Issues and PRs related to ECMAScript promises. question Issues that look for answers. labels Nov 21, 2017
@addaleax
Copy link
Member

From https://nodejs.org/api/timers.html#timers_settimeout_callback_delay_args:

Node.js makes no guarantees about the exact timing of when callbacks will fire, nor of their ordering.

@vsemozhetbyt
Copy link
Contributor

See also #15081

@bnoordhuis
Copy link
Member

Answered, closing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
promises Issues and PRs related to ECMAScript promises. question Issues that look for answers.
Projects
None yet
Development

No branches or pull requests

4 participants