如果您用另一个承诺解决承诺,该怎么办? [英] What should happen if you resolve a promise with another promise?
问题描述
ECMAScript 6.0规范对于 Promise
解决另一个 Promise
是什么?应该通过将然后
附加到 Promise
中的另一个 Promise
code>这将解决这个问题?
What does the ECMAScript 6.0 specification say about a Promise
resolving another Promise
? Should it adopt the state of the other Promise
by attaching a then
to that Promise
which would resolve this one?
我在Chrome中尝试过这个代码段,这里是我得到的,似乎只是解决了 Promise1
与 Promise2
是不是很好?
I tried this snippet in Chrome and here is what I get and it seems to just resolve the Promise1
with Promise2
is that fine?
> Promise1 = new Promise(function(resolve){ResolveYouLater1 = resolve})
> Promise2 = new Promise(function(resolve){ResolveYouLater2 = resolve})
> ResolveYouLater1(Promise2)
undefined
> Promise1
Promise {[[PromiseStatus]]: "resolved", [[PromiseValue]]: Promise}
我认为它应该附加一个然后
到Promise2,应该 ResolveYouLater1(value)
当 Promise2
是定居的。
I thought it should attach a then
to Promise2 which should ResolveYouLater1(value)
when Promise2
is the settled.
推荐答案
什么是ECMAScript 6.0规范说明承诺解决另一个承诺?
What does the ECMAScript 6.0 specification say about a Promise resolving another Promise?
您可以在这里阅读: http://www.ecma-international.org/ecma-262/6.0/#sec- promise-resolve-functions
当参数是可写的时候,一个 PromiseResolveThenableJob 将排队等待。
You can read it yourself here: http://www.ecma-international.org/ecma-262/6.0/#sec-promise-resolve-functions
When the argument is a thenable, a PromiseResolveThenableJob will be queued.
应该采用其他Promise的状态附在母鸡要承诺解决这个问题?
Should it adopt the state of the other Promise by attaching a then to that Promise which would resolve this one?
是的。将使用解析器和拒绝函数调用 .then()
方法。
Yes. The .then()
method will be called with a resolver and rejecter function.
我在Chrome中尝试过这个代码片段,这里是我得到的,似乎只是解决Promise2与Promise2是好的?
I tried this snippet in Chrome and here is what I get and it seems to just resolve the Promise1 with Promise2 is that fine?
是。现在只有解决。
我必须承认,我目前的Chrome版本(48.0.2564.82,V8 4.8.271.17)并不完全符合这个算法(这可能不是一件坏事,而且避免内存问题,但可能是意外的)。
这是懒惰的,只有在实际回调后才安排该工作对结果感兴趣而且它不会通过解析器到然后
方法,而是想要了解结果的实际回调。
I do have to admit, my current Chrome Version (48.0.2564.82, V8 4.8.271.17) doesn't exactly comply to this algorithm (which might not be a bad thing per se, and avoids memory problems, but can be unexpected).
It is lazy, and only schedules the job once an actual callback is interested in the result. And it doesn't pass a resolver to the then
method, but the actual callback that wants to know the result.
> 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
这篇关于如果您用另一个承诺解决承诺,该怎么办?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!