ECMAScript 6.0規範對Promise解析另一個Promise有何評論?
您可以自己閱讀這裏:http://www.ecma-international.org/ecma-262/6.0/#sec-promise-resolve-functions
當參數是thenable,一個PromiseResolveThenableJob將排隊。
它是否應該採用另一個Promise的狀態,通過附加一個then來解析這個Promise?
是。 .then()
方法將使用解析器和拒絕函數來調用。
我在Chrome中試過這段代碼,這裏是我得到的,它似乎只是用Promise2來解決Promise1很好嗎?
是。現在只有resolved。
我必須承認,我目前的Chrome版本(48.0.2564.82,V8 4.8.271.17)並不完全符合這個算法(它可能不是一個壞事情本身,而avoids memory problems,但可能是意想不到的)。
這是懶惰的,只有在實際回調對結果感興趣時才安排工作。並且它不會將解析器傳遞給then
方法,而是想知道結果的實際回調。
> var resolve1, resolve2;
> var promise1 = new Promise(function(r){ resolve1 = r; });
> var promise2 = new Promise(function(r){ resolve2 = r; });
> promise2.then = function(...args) { console.log(...args);
> return Promise.prototype.then.call(this, ...args); };
> resolve1(promise2);
undefined
// you'd expected a call to the then method here
> promise1
Promise {[[PromiseStatus]]: "resolved", [[PromiseValue]]: Promise}
> promise2
Promise {[[PromiseStatus]]: "pending", [[PromiseValue]]: undefined}
> resolve2(42)
undefined
// or at least here
> promise1
Promise {[[PromiseStatus]]: "resolved", [[PromiseValue]]: Promise}
> promise2
Promise {[[PromiseStatus]]: "resolved", [[PromiseValue]]: 42}
> promise1.then(x => console.log(x))
x => console.log(x), PromiseIdRejectHandler() { [native code] }
// but it only is here
42
Promise {[[PromiseStatus]]: "pending", [[PromiseValue]]: undefined}
> promise2.then(x => console.log(x))
x => console.log(x)
42
Promise {[[PromiseStatus]]: "pending", [[PromiseValue]]: undefined}
> promise1.then(x => console.log(x))
x => console.log(x), PromiseIdRejectHandler() { [native code] }
// hell they're even calling it multiple times!
42
Promise {[[PromiseStatus]]: "pending", [[PromiseValue]]: undefined}
> var resolve1, resolve2;
> var promise1 = new Promise(function(r){ resolve1 = r; });
> var thenable = {then:function(r) { console.log(...arguments); resolve2 = r; }};
> resolve1(thenable);
undefined
// you'd expected a call to the then method here
> promise1
Promise {[[PromiseStatus]]: "resolved", [[PromiseValue]]: Object}
> thenable
Object {}
> resolve2(42)
Uncaught TypeError: resolve2 is not a function(…)
// uh. yeah.
> promise1.then(x => console.log(x))
() { [native code] },() { [native code] }
// ah there was the call, even with a resolver
Promise {[[PromiseStatus]]: "pending", [[PromiseValue]]: undefined}
> resolve2(42)
42
undefined
> promise1.then(x => console.log(x))
() { [native code] },() { [native code] }
// but now they fucked up.
// Instead of another call on the thenable, you'd expected the result to be logged
Promise {[[PromiseStatus]]: "pending", [[PromiseValue]]: undefined}
> resolve2(42)
42
// OMG.
undefined
請不要通過內部成員的控制檯上的輸出被愚弄。 Promise1將以與Promise2相同的價格結算。它還沒有完成。這是[解決](http://stackoverflow.com/a/29269515/1048572)與現在的其他承諾,這很好。 – Bergi
爲什麼Chrome控制檯會給我這個值?這是有原因的嗎?我會在node.js中試用它。你的意思是它會附加一個然後對Promise的權利(如後面第二種情況所述)?此外,Promise1的時間將在何時開始,因爲您說過它會與其他Promise一起解決。 – Nishant
在調用'ResolveYouLater1(Promise2)'之前,你可以嘗試用一個攔截器(記錄一條消息)替換'Promise2.then',你應該看到它如何附加一個處理器。 – Bergi