一天愜意的下午。朋友給我分享了一道頭條面試題,以下:node
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')
複製代碼複製代碼
這個題目主要是考察對同步任務、異步任務:setTimeout、promise、async/await的執行順序的理解程度。(建議你們也本身先作一下o)面試
當時因爲我對async、await瞭解的不是很清楚,答案錯的千奇百怪 :(),就不記錄了,而後我就去看文章理了理思路。如今寫在下面以供往後參考。promise
這裏首先須要明白幾個概念:同步任務、異步任務、任務隊列、microtask、macrotaskbash
總結一下:數據結構
task queue、microtask、macrotask異步
- An event loop has one or more task queues.(task queue is macrotask queue)
- Each event loop has a microtask queue.
- task queue = macrotask queue != microtask queue
- a task may be pushed into macrotask queue,or microtask queue
- when a task is pushed into a queue(micro/macro),we mean preparing work is finished,so the task can be executed now.
因此咱們能夠獲得js執行順序是:async
開始 -> 取第一個task queue裏的任務執行(能夠認爲同步任務隊列是第一個task queue) -> 取microtask所有任務依次執行 -> 取下一個task queue裏的任務執行 -> 再次取出microtask所有任務執行 -> … 這樣循環往復函數
macrotask:oop
microtask:post
不記得題的!繼續往下看,舒適的準備了題目,不用往上翻
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')
複製代碼複製代碼
node環境下: script start -> async1 start -> async2 -> promise1 -> script end -> promise2 -> async1 end -> setTimeout
Chrome環境下: script start -> async1 start -> async2 -> promise1 -> script end -> async1 end -> promise2 -> setTimeout
按照上面寫的js執行順序就能夠獲得正確結果,但最後卻又存在兩個答案,爲何會出現兩種結果呢?咱們能夠看到兩種結果中就是async1 end 和 Promise2之間的順序出現差異,這主要是V8最新版本與稍老版本的差別,他們對await的執行方法不一樣,以下:
async function f(){
await p
console.log(1);
}
//新版V8應該會解析成下面這樣
function f(){
Promise.resolve(p).then(()=>{
console.log(1)
})
}
//舊版的V8應該會解析成下面的這樣
function f(){
new Promise(resolve=>{
resolve(p)
}).then(()=>{
console.log(1)
})
}
複製代碼複製代碼
正對上面的這兩種差別主要是:
不用擔憂這個題沒解,真相只有一個。根據 TC39 最近決議,await將直接使用 Promise.resolve() 相同語義。
最後咱們以最新決議來分析這個題目的可能的執行過程:
stackoverflow上的一道題目
let resolvePromise = new Promise(resolve => {
let resolvedPromise = Promise.resolve()
resolve(resolvedPromise)
})
resolvePromise.then(() => {
console.log('resolvePromise resolved')
})
let resolvedPromiseThen = Promise.resolve().then(res => {
console.log('promise1')
})
resolvedPromiseThen
.then(() => {
console.log('promise2')
})
.then(() => {
console.log('promise3')
})
複製代碼複製代碼
結果:promise1 -> promise2 -> resolvePromise resolved -> promise3
這道題真的是很是費解了。爲何'resolvePromise resolved'會在第三行才顯示呢?和舍友討論了一夜無果。
其實這個題目的難點就在於resolve一個Promise對象,js引擎會怎麼處理。咱們知道Promise.resolve()的參數爲Promise對象時,會直接返回這個Promise對象。但當resolve()的參數爲Promise對象時,狀況會有所不一樣:
resolve(resolvedPromise)
//等同於:
Promise.resolve().then(() => resolvedPromise.then(resolve, reject));
複製代碼複製代碼
因此這裏第一次執行到這兒的時候:
() => resolvedPromise.then(resolve, reject)
會被放入當前任務列表的最後結束!這裏面的幾段代碼是比較重要的,解釋了js會按照什麼樣的方式來執行這些新特性。
最後若是有誤,歡迎指正。