// Multi-unwrapped chaining with thenable coercion.
function PromiseThen(onResolve, onReject) {
- onResolve = IS_UNDEFINED(onResolve) ? PromiseIdResolveHandler : onResolve;
+ onResolve =
+ IS_NULL_OR_UNDEFINED(onResolve) ? PromiseIdResolveHandler : onResolve;
+ onReject =
+ IS_NULL_OR_UNDEFINED(onReject) ? PromiseIdRejectHandler : onReject;
var that = this;
var constructor = this.constructor;
return this.chain(
var then;
try {
then = x.then;
- } catch(e) {
- var deferred = %_CallFunction(constructor, PromiseDeferred);
- PromiseCoerce.table.set(x, deferred.promise);
- deferred.reject(e);
- return deferred.promise;
+ } catch(r) {
+ var promise = %_CallFunction(constructor, r, PromiseRejected);
+ PromiseCoerce.table.set(x, promise);
+ return promise;
}
if (typeof then === 'function') {
if (PromiseCoerce.table.has(x)) {
PromiseCoerce.table.set(x, deferred.promise);
try {
%_CallFunction(x, deferred.resolve, deferred.reject, then);
- } catch(e) {
- deferred.reject(e);
+ } catch(r) {
+ deferred.reject(r);
}
return deferred.promise;
}
(function() {
Promise.resolve(5).then(undefined, assertUnreachable).chain(
- function(x) { assertAsync(x === 5, "resolved/then-nohandler") },
+ function(x) { assertAsync(x === 5, "resolved/then-nohandler-undefined") },
+ assertUnreachable
+ )
+ assertAsyncRan()
+ Promise.resolve(6).then(null, assertUnreachable).chain(
+ function(x) { assertAsync(x === 6, "resolved/then-nohandler-null") },
assertUnreachable
)
assertAsyncRan()
(function() {
Promise.reject(5).then(assertUnreachable, undefined).chain(
assertUnreachable,
- function(r) { assertAsync(r === 5, "rejected/then-nohandler") }
+ function(r) { assertAsync(r === 5, "rejected/then-nohandler-undefined") }
+ )
+ assertAsyncRan()
+ Promise.reject(6).then(assertUnreachable, null).chain(
+ assertUnreachable,
+ function(r) { assertAsync(r === 6, "rejected/then-nohandler-null") }
)
assertAsyncRan()
})();