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 执行结果:
clipboard.png

p2 是 then() 所返回的 Promise,初始状态为 pending,后边并没有 resolve,应该一直保持 pending 状态才对。firefox 的表现是正确的。不知道为什么 chrome 会显示状态为 resolved ?

阅读 2.9k
2 个回答

你要知道 chrome 控制台里打印的对象都是引用, 在你展开时才通过引用求值.

在你 console.log('promise2: ', p2) 执行的这一刻 [[PromiseStatus]] 的值的确是 'pending', 但是当你鼠标移动点击展开时, p2 已经变成 resolve 状态了, 此时你当然看不到 'pending' 这个中间态了, 你可以在 console.log('promise2: ', p2) 后加一个 debugger 语句, 就可以在控制台看到 p2 此刻的状态了.

瞬时状态:
clipboard.png

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...
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进