2016-01-23 84 views
1

ECMAScript 6.0規範對Promise解決了另一個問題Promise有何看法?是否應該通過附加thenPromise來解決這個問題來採用其他Promise的狀態?如果你用另一個承諾解決承諾會發生什麼?

我在Chrome中試過這段代碼,這裏是我得到的,它似乎只是解決了Promise1Promise2是好的?

> Promise1 = new Promise(function(resolve){ResolveYouLater1 = resolve}) 
> Promise2 = new Promise(function(resolve){ResolveYouLater2 = resolve}) 
> ResolveYouLater1(Promise2) 
    undefined 
> Promise1 
    Promise {[[PromiseStatus]]: "resolved", [[PromiseValue]]: Promise} 

我認爲應該附加一個then到Promise2這應該ResolveYouLater1(value)Promise2是塵埃落定。

+1

請不要通過內部成員的控制檯上的輸出被愚弄。 Promise1將以與Promise2相同的價格結算。它還沒有完成。這是[解決](http://stackoverflow.com/a/29269515/1048572)與現在的其他承諾,這很好。 – Bergi

+0

爲什麼Chrome控制檯會給我這個值?這是有原因的嗎?我會在node.js中試用它。你的意思是它會附加一個然後對Promise的權利(如後面第二種情況所述)?此外,Promise1的時間將在何時開始,因爲您說過它會與其他Promise一起解決。 – Nishant

+1

在調用'ResolveYouLater1(Promise2)'之前,你可以嘗試用一個攔截器(記錄一條消息)替換'Promise2.then',你應該看到它如何附加一個處理器。 – Bergi

回答

1

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 
+0

輝煌。我會通過這個! – Nishant

+0

此外,我添加了返回新的然後方法,以便它正確鏈接。我試了一下,發現這個錯過了。請讓我知道,如果這不正確或任何我很新。 – Nishant

+1

是的,採用另一種承諾意味着兩者都解決相同的價值,那麼承諾幾乎不可區分。採用可靠性是通過傳遞'resolve'和'reject'回調來解決'then()'方法的承諾。委託Chrome等所有「隨後」調用並不符合規範。 – Bergi