async1().then(() => {
console.log('async1 call')
})
sync2().then(()=>{
console.log('sync2 call')
})
async function async1() {
return Promise.resolve(new Promise(resolve => {
console.log('async1 resolve')
resolve()
})
)
}
function sync2() {
return Promise.resolve(new Promise(resolve => {
console.log('sync2 resolve')
resolve()
})
)
}
new Promise(resolve => {
console.log('Promise3 resolve')
resolve()
}).then(() => {
console.log('Promise3 call')
})
console.log('script end')
上面的代码输出顺序为:
这是为啥呢?个人理解按微观队列的先进先出原则,'async1 call' 应该是在 'sync2 call' 之前输入才对啊,我发现只要去掉async1方法的async修饰符,一切就又正常了。是async修饰符对微观队列的插入顺序起了什么特殊影响吗?有没有哪位大佬帮小弟解答下
关键不在于
async
,而在于await
来看看「理解 JavaScript 的 async/await」