在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問(wèn)答/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,后邊并沒(méi)有 resolve,應(yīng)該一直保持 pending 狀態(tài)才對(duì)。firefox 的表現(xiàn)是正確的。不知道為什么 chrome 會(huì)顯示狀態(tài)為 resolved ?

回答
編輯回答
有點(diǎn)壞
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 控制臺(tái)里打印的對(duì)象都是引用, 在你展開(kāi)時(shí)才通過(guò)引用求值.

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

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

2017年1月24日 22:24