async function async1(){
console.log('async1 start')//2
await async2() //await后面都作為回調(diào)內(nèi)容 微任務(wù)1=>放在微任務(wù)隊列
console.log('async1 end')
}
async function async2() {
console.log('async2')//3
}
console.log('script start')//1
setTimeout(() => { //宏任務(wù) setTimeout =>callback queue
console.log('setTimeout')
});
async1()//
//初始化Promise是涣澡,傳入的函數(shù)會立刻被執(zhí)行
new Promise((resolve=>{
console.log('promise1')//4
resolve()
})).then(function () { //微任務(wù)2=>放在微任務(wù)隊列
console.log('promise2')
})
console.log('script end')//5
結(jié)果:
script start.
async1 start.
async2.
promise1.
script end
//同步代碼執(zhí)行完畢 call stack被清空----
//開始執(zhí)行微任務(wù)----
async1 end.
promise2.
//(嘗試出觸發(fā)DOM渲染)
觸發(fā)Event Loop ,執(zhí)行宏任務(wù)------
setTimeout