Index: src/js/promise.js |
diff --git a/src/js/promise.js b/src/js/promise.js |
index 466912d5eb6eaba98a8e4d9fe41c90ead55c4667..20ffc8c4892327ac68f5242c7900a1006303959e 100644 |
--- a/src/js/promise.js |
+++ b/src/js/promise.js |
@@ -390,40 +390,47 @@ function PromiseCreateResolved(x) { |
return %_Call(PromiseResolve, GlobalPromise, x); |
} |
-// ES#sec-promise.prototype.then |
-// Promise.prototype.then ( onFulfilled, onRejected ) |
-// Multi-unwrapped chaining with thenable coercion. |
-function PromiseThen(onResolve, onReject) { |
- var status = GET_PRIVATE(this, promiseStateSymbol); |
- if (IS_UNDEFINED(status)) { |
- throw MakeTypeError(kNotAPromise, this); |
- } |
+function PerformPromiseThen(promise, onResolve, onReject, resultCapability) { |
+ if (!IS_CALLABLE(onResolve)) onResolve = PromiseIdResolveHandler; |
+ if (!IS_CALLABLE(onReject)) onReject = PromiseIdRejectHandler; |
- var constructor = SpeciesConstructor(this, GlobalPromise); |
- onResolve = IS_CALLABLE(onResolve) ? onResolve : PromiseIdResolveHandler; |
- onReject = IS_CALLABLE(onReject) ? onReject : PromiseIdRejectHandler; |
- var deferred = NewPromiseCapability(constructor); |
+ var status = GET_PRIVATE(promise, promiseStateSymbol); |
switch (status) { |
case kPending: |
- PromiseAttachCallbacks(this, deferred, onResolve, onReject); |
+ PromiseAttachCallbacks(promise, resultCapability, onResolve, onReject); |
break; |
case kFulfilled: |
- PromiseEnqueue(GET_PRIVATE(this, promiseResultSymbol), |
- onResolve, deferred, kFulfilled); |
+ PromiseEnqueue(GET_PRIVATE(promise, promiseResultSymbol), |
+ onResolve, resultCapability, kFulfilled); |
break; |
case kRejected: |
- if (!HAS_DEFINED_PRIVATE(this, promiseHasHandlerSymbol)) { |
+ if (!HAS_DEFINED_PRIVATE(promise, promiseHasHandlerSymbol)) { |
// Promise has already been rejected, but had no handler. |
// Revoke previously triggered reject event. |
- %PromiseRevokeReject(this); |
+ %PromiseRevokeReject(promise); |
} |
- PromiseEnqueue(GET_PRIVATE(this, promiseResultSymbol), |
- onReject, deferred, kRejected); |
+ PromiseEnqueue(GET_PRIVATE(promise, promiseResultSymbol), |
+ onReject, resultCapability, kRejected); |
break; |
} |
+ |
// Mark this promise as having handler. |
- SET_PRIVATE(this, promiseHasHandlerSymbol, true); |
- return deferred.promise; |
+ SET_PRIVATE(promise, promiseHasHandlerSymbol, true); |
+ return resultCapability.promise; |
+} |
+ |
+// ES#sec-promise.prototype.then |
+// Promise.prototype.then ( onFulfilled, onRejected ) |
+// Multi-unwrapped chaining with thenable coercion. |
+function PromiseThen(onResolve, onReject) { |
+ var status = GET_PRIVATE(this, promiseStateSymbol); |
+ if (IS_UNDEFINED(status)) { |
+ throw MakeTypeError(kNotAPromise, this); |
+ } |
+ |
+ var constructor = SpeciesConstructor(this, GlobalPromise); |
+ var resultCapability = NewPromiseCapability(constructor); |
+ return PerformPromiseThen(this, onResolve, onReject, resultCapability); |
} |
// Unspecified V8-specific legacy function |
@@ -622,6 +629,8 @@ utils.InstallFunctions(extrasUtils, 0, [ |
fn => %FunctionRemovePrototype(fn)); |
utils.Export(function(to) { |
+ to.IsPromise = IsPromise; |
+ |
to.PromiseChain = PromiseChain; |
to.PromiseDefer = PromiseDefer; |
to.PromiseAccept = PromiseAccept; |
@@ -629,6 +638,10 @@ utils.Export(function(to) { |
to.PromiseCreateRejected = PromiseCreateRejected; |
to.PromiseCreateResolved = PromiseCreateResolved; |
to.PromiseThen = PromiseThen; |
+ |
+ to.GlobalPromise = GlobalPromise; |
+ to.NewPromiseCapability = NewPromiseCapability; |
+ to.PerformPromiseThen = PerformPromiseThen; |
}); |
}) |