Front-end-learning-to-organize-notes icon indicating copy to clipboard operation
Front-end-learning-to-organize-notes copied to clipboard

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

Open Chocolate1999 opened this issue 5 years ago • 1 comments

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 avatar Aug 24 '20 12:08 Chocolate1999

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

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

jcyicai avatar Feb 22 '21 05:02 jcyicai