见代码:
async function async1() {
console.log('async1 start')
await async2();
console.log('async1 end')
}
async function async2() {
console.log('async2')
}
console.log('script start')
setTimeout(function () {
console.log('setTimeout')
}, 0)
async1();
new Promise(function (resolve) {
console.log('promise1')
resolve();
}).then(function () {
console.log('promise2')
})
console.log('script end')
输出结果:
script start
async1 start
async2
promise1
script end
async1 end
promise2
setTimeout
问题:
为什么在输出async2后不是返回执行async1 end 而是跳到了console.log('promise1')这里。
问题可能问得比较小白哈哈哈
await 的意思是等待,它相当于then回调,前面的都是同步代码,从这里'async1 end'被加入到微任务列表