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();
该题执行顺序为:
- 执行firstFunction()函数,进入函数执行MyPromise() ,然后将resolve()放入微队列,打印 second
- 继续执行 secondFunction()函数,执行await后面语句,mypromise()放入微队列,后续代码也放入。
此时同步任务执行完成,执行微任务队列中的任务因此最终结果为 second, I have resolved, I have resolved, second
,所以答案为 D