鍍金池/ 問答/HTML/ promise 在 chrome 和 firefox 中的差異

promise 在 chrome 和 firefox 中的差異

let p1 = new Promise(resolve => {
    resolve('promise1 resolved');
})    
        
var p2 = p1.then(function(res){});

console.log('promise2: ',p2);

chrome:{} 里邊顯示 pending,而下邊的 [[PromiseStatus]] 顯示 resolved
clipboard.png

firefox 執(zhí)行結(jié)果:
clipboard.png

p2 是 then() 所返回的 Promise,初始狀態(tài)為 pending,后邊并沒有 resolve,應(yīng)該一直保持 pending 狀態(tài)才對。firefox 的表現(xiàn)是正確的。不知道為什么 chrome 會顯示狀態(tài)為 resolved ?

回答
編輯回答
有點壞
returns a value, the promise returned by then gets resolved with the returned value as its value;
throws an error, the promise returned by then gets rejected with the thrown error as its value;
returns an already resolved promise, the promise returned by then gets resolved with that promise's value as its value;
returns an already rejected promise, the promise returned by then gets rejected with that promise's value as its value.
returns another pending promise object, the resolution/rejection of the promise returned by then will be subsequent to the resolution/rejection of the promise returned by the handler. Also, the value of the promise returned by then will be the same as the value of the promise returned by the handler.
from here: https://developer.mozilla.org...
2017年12月16日 03:08
編輯回答
薄荷糖

你要知道 chrome 控制臺里打印的對象都是引用, 在你展開時才通過引用求值.

在你 console.log('promise2: ', p2) 執(zhí)行的這一刻 [[PromiseStatus]] 的值的確是 'pending', 但是當(dāng)你鼠標移動點擊展開時, p2 已經(jīng)變成 resolve 狀態(tài)了, 此時你當(dāng)然看不到 'pending' 這個中間態(tài)了, 你可以在 console.log('promise2: ', p2) 后加一個 debugger 語句, 就可以在控制臺看到 p2 此刻的狀態(tài)了.

瞬時狀態(tài):
clipboard.png

2017年1月24日 22:24