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

【挑战系列1】以下代码输出结果 #3

Open
Chocolate1999 opened this issue Aug 24, 2020 · 1 comment
Open

【挑战系列1】以下代码输出结果 #3

Chocolate1999 opened this issue Aug 24, 2020 · 1 comment

Comments

@Chocolate1999
Copy link
Owner

console.log(1);
setTimeout(() => {
  console.log(2);
  process.nextTick(() => {
    console.log(3);
  });
  new Promise((resolve) => {
    console.log(4);
    resolve();
  }).then(() => {
    console.log(5);
  });
});
new Promise((resolve) => {
  console.log(7);
  resolve();
}).then(() => {
  console.log(8);
});
process.nextTick(() => {
  console.log(6);
});
setTimeout(() => {
  console.log(9);
  process.nextTick(() => {
    console.log(10);
  });
  new Promise((resolve) => {
    console.log(11);
    resolve();
  }).then(() => {
    console.log(12);
  });
});
@Chocolate1999 Chocolate1999 changed the title 【挑战系列】以下代码输出结果 【挑战系列1】以下代码输出结果 Aug 24, 2020
@jcyicai
Copy link

jcyicai commented Feb 22, 2021

1
7
6
8
2
4
3
5
9
11
10
12

分析:
1. promise resolve之前是同步的,then才是异步的
2. process.nextTick优先于setTimeout、promise.then

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants