Skip to content

【Promise第38题】下面代码的输出是什么?

Posted on:2024年7月20日 at 11:01
const first = () =>
  new Promise((resolve, reject) => {
    console.log(3);
    let p = new Promise((resolve, reject) => {
      console.log(7);
      setTimeout(() => {
        console.log(5);
        resolve(6);
        console.log(p);
      }, 0);
      resolve(1);
    });
    resolve(2);
    p.then((arg) => {
      console.log(arg);
    });
  });
first().then((arg) => {
  console.log(arg);
});
console.log(4);

解析

结果

3
7
4
1
2
5
Promise{<resolved>: 1}
原文转自:https://fe.ecool.fun/topic/570eed0a-2340-4c59-a6c0-967db6a7dc48