async function async1() {
async2().then(() => {
console.log('async1 end');
});
}
async function async2() {
return new Promise((resolve, reject) => {
resolve();
})
}
async1();
new Promise(function(resolve) {
resolve();
}).then(function() {
console.log('promise2');
})
输出结果为
promise2
async1 end
(面试时候遇到的,去掉了无关的 log)
想知道为什么 promise2 早于 async1 end ?
我的想法是 async2() 的 then 方法早于 new Promise 的 then 方法执行,那么其回调函数就应该先被放入微任务队列,也就应该先被执行,但实际情况不是这样。
另外,如果我把 async1 和 async2 的 async 关键字去掉,那么输出就变为
async1 end
promise2
求大佬解答