Skip to content

依次输出什么?

Posted on:2023年3月4日 at 22:52
const myPromise = () => Promise.resolve("I have resolved!");

function firstFunction() {
  myPromise().then((res) => console.log(res));
  console.log("second");
}

async function secondFunction() {
  console.log(await myPromise());
  console.log("second");
}

firstFunction();
secondFunction();

该题执行顺序为:

此时同步任务执行完成,执行微任务队列中的任务因此最终结果为 second, I have resolved, I have resolved, second,所以答案为 D

原文转自:https://fe.ecool.fun/topic/fe86322b-4990-4f6b-a473-d37a8e3ecb2d