What happens if you resolve the promise with another promise?

What does the ECMAScript 6.0 specification say about Promisesolving another Promise? Should he accept the state of another Promiseby joining thenthis Promisethat would allow it?

I tried this snippet in Chrome and this is what I get and it seems that it just resolves Promise1with Promise2, is this fine?

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

I thought that it should attach thento Promise2, which should ResolveYouLater1(value), when Promise2installed.

+1
source share
1 answer

ECMAScript 6.0 , ?

: http://www.ecma-international.org/ecma-262/6.0/#sec-promise-resolve-functions
thenable, PromiseResolveThenableJob .

, , ?

. .then() resolver rejecter.

Chrome, , , Promise1 Promise2, ?

. .


, Chrome (48.0.2564.82, V8 4.8.271.17) (, , , , ).
, . 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
+1

Source: https://habr.com/ru/post/1681691/


All Articles