Index: src/js/promise.js |
diff --git a/src/js/promise.js b/src/js/promise.js |
index 6fc8728562c03f4e5def5bc4d3dcc72088b90b75..6248654ecc28c75c36eb7afb13e91c9941eb88c4 100644 |
--- a/src/js/promise.js |
+++ b/src/js/promise.js |
@@ -227,15 +227,22 @@ function NewPromiseCapability(C) { |
resolve: callbacks.resolve, |
reject: callbacks.reject |
}; |
- } else { |
- var result = {promise: UNDEFINED, resolve: UNDEFINED, reject: UNDEFINED }; |
- result.promise = new C(function(resolve, reject) { |
- // TODO(littledan): Check for resolve and reject being not undefined |
- result.resolve = resolve; |
- result.reject = reject; |
- }); |
- return result; |
} |
+ |
+ var result = {promise: UNDEFINED, resolve: UNDEFINED, reject: UNDEFINED }; |
+ result.promise = new C(function(resolve, reject) { |
+ if (!IS_UNDEFINED(result.resolve) || !IS_UNDEFINED(result.reject)) |
+ throw MakeTypeError(kPromiseExecutorAlreadyInvoked); |
+ result.resolve = resolve; |
+ result.reject = reject; |
+ }); |
+ |
+ if (!IS_CALLABLE(result.resolve)) |
+ throw MakeTypeError(kCalledNonCallable, "promiseCapability.[[Resolve]]"); |
+ if (!IS_CALLABLE(result.reject)) |
+ throw MakeTypeError(kCalledNonCallable, "promiseCapability.[[Reject]]"); |
+ |
+ return result; |
} |
function PromiseDeferred() { |
@@ -250,17 +257,18 @@ function PromiseRejected(r) { |
if (!IS_RECEIVER(this)) { |
throw MakeTypeError(kCalledOnNonObject, PromiseRejected); |
} |
- var promise; |
if (this === GlobalPromise) { |
// Optimized case, avoid extra closure. |
- promise = PromiseCreateAndSet(-1, r); |
+ var promise = PromiseCreateAndSet(-1, r); |
// The debug event for this would always be an uncaught promise reject, |
// which is usually simply noise. Do not trigger that debug event. |
%PromiseRejectEvent(promise, r, false); |
+ return promise; |
} else { |
- promise = new this(function(resolve, reject) { reject(r) }); |
+ var promiseCapability = NewPromiseCapability(this); |
+ %_Call(promiseCapability.reject, UNDEFINED, r); |
adamk
2016/01/05 20:33:09
Any reason to use %_Call here instead of just maki
caitp (gmail)
2016/01/05 20:39:54
The `promiseCapability` object is emulating a Reco
adamk
2016/01/05 20:44:41
You could do:
var reject = promiseCapability.reje
|
+ return promiseCapability.promise; |
} |
- return promise; |
} |
// Multi-unwrapped chaining with thenable coercion. |
@@ -359,6 +367,10 @@ function PromiseAll(iterable) { |
} |
function PromiseRace(iterable) { |
+ if (!IS_RECEIVER(this)) { |
+ throw MakeTypeError(kCalledOnNonObject, PromiseRace); |
+ } |
+ |
var deferred = NewPromiseCapability(this); |
try { |
for (var value of iterable) { |