Skip to content

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

Posted on:2022年1月9日 at 23:24
const async1 = async () => {
  console.log("async1");
  setTimeout(() => {
    console.log("timer1");
  }, 2000);
  await new Promise((resolve) => {
    console.log("promise1");
  });
  console.log("async1 end");
  return "async1 success";
};
console.log("script start");
async1().then((res) => console.log(res));
console.log("script end");
Promise.resolve(1)
  .then(2)
  .then(Promise.resolve(3))
  .catch(4)
  .then((res) => console.log(res));
setTimeout(() => {
  console.log("timer2");
}, 1000);

解析

需要注意的点:

结果

'script start'
'async1'
'promise1'
'script end'
1
'timer2'
'timer1'

原文转自:https://fe.ecool.fun/topic/41c2b7e0-3ab2-4a94-8017-1804b0ddf868