Promises: some adaptations to spec

- Rename Promise.{resolved,rejected,deferred} to Promise.{resolve,reject,defer}
- Rename Promise.one to Promise.race
- Make all failures asynchronous, EXCEPT type errors for resolver
- Disallow non-construct call to Promise constructor
- Don't make combinators go through public this.defer

Also, don't bother using IsCallable.

R=dslomov@chromium.org, yhirano@chromium.org
BUG=

Review URL: https://codereview.chromium.org/99573002

git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@18515 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
This commit is contained in:
rossberg@chromium.org 2014-01-09 13:00:56 +00:00
parent 61692bf294
commit 3286bc71e3
5 changed files with 265 additions and 226 deletions

View File

@ -110,6 +110,7 @@ var kMessages = {
data_view_not_array_buffer: ["First argument to DataView constructor must be an ArrayBuffer"], data_view_not_array_buffer: ["First argument to DataView constructor must be an ArrayBuffer"],
constructor_not_function: ["Constructor ", "%0", " requires 'new'"], constructor_not_function: ["Constructor ", "%0", " requires 'new'"],
not_a_promise: ["%0", " is not a promise"], not_a_promise: ["%0", " is not a promise"],
resolver_not_a_function: ["Promise resolver ", "%0", " is not a function"],
promise_cyclic: ["Chaining cycle detected for promise ", "%0"], promise_cyclic: ["Chaining cycle detected for promise ", "%0"],
array_functions_on_frozen: ["Cannot modify frozen array elements"], array_functions_on_frozen: ["Cannot modify frozen array elements"],
array_functions_change_sealed: ["Cannot add/remove sealed array elements"], array_functions_change_sealed: ["Cannot add/remove sealed array elements"],

View File

@ -62,11 +62,16 @@ function IsPromise(x) {
function Promise(resolver) { function Promise(resolver) {
if (resolver === promiseRaw) return; if (resolver === promiseRaw) return;
if (!%_IsConstructCall()) throw MakeTypeError('not_a_promise', [this]);
if (typeof resolver !== 'function')
throw MakeTypeError('resolver_not_a_function', [resolver]);
var promise = PromiseInit(this); var promise = PromiseInit(this);
try {
resolver(function(x) { PromiseResolve(promise, x) }, resolver(function(x) { PromiseResolve(promise, x) },
function(r) { PromiseReject(promise, r) }); function(r) { PromiseReject(promise, r) });
// TODO(rossberg): current draft makes exception from this call asynchronous, } catch (e) {
// but that's probably a mistake. PromiseReject(promise, e);
}
} }
function PromiseSet(promise, status, value, onResolve, onReject) { function PromiseSet(promise, status, value, onResolve, onReject) {
@ -82,10 +87,11 @@ function PromiseInit(promise) {
} }
function PromiseDone(promise, status, value, promiseQueue) { function PromiseDone(promise, status, value, promiseQueue) {
if (GET_PRIVATE(promise, promiseStatus) !== 0) return; if (GET_PRIVATE(promise, promiseStatus) === 0) {
PromiseEnqueue(value, GET_PRIVATE(promise, promiseQueue)); PromiseEnqueue(value, GET_PRIVATE(promise, promiseQueue));
PromiseSet(promise, status, value); PromiseSet(promise, status, value);
} }
}
function PromiseResolve(promise, x) { function PromiseResolve(promise, x) {
PromiseDone(promise, +1, x, promiseOnResolve) PromiseDone(promise, +1, x, promiseOnResolve)
@ -219,14 +225,13 @@ function PromiseThen(onResolve, onReject) {
PromiseCoerce.table = new $WeakMap; PromiseCoerce.table = new $WeakMap;
function PromiseCoerce(constructor, x) { function PromiseCoerce(constructor, x) {
var then; if (!(IsPromise(x) || IS_NULL_OR_UNDEFINED(x))) {
if (IsPromise(x)) { var then = x.then;
return x; if (typeof then === 'function') {
} else if (!IS_NULL_OR_UNDEFINED(x) && %IsCallable(then = x.then)) {
if (PromiseCoerce.table.has(x)) { if (PromiseCoerce.table.has(x)) {
return PromiseCoerce.table.get(x); return PromiseCoerce.table.get(x);
} else { } else {
var deferred = constructor.deferred(); var deferred = %_CallFunction(constructor, PromiseDeferred);
PromiseCoerce.table.set(x, deferred.promise); PromiseCoerce.table.set(x, deferred.promise);
try { try {
%_CallFunction(x, deferred.resolve, deferred.reject, then); %_CallFunction(x, deferred.resolve, deferred.reject, then);
@ -235,22 +240,23 @@ function PromiseCoerce(constructor, x) {
} }
return deferred.promise; return deferred.promise;
} }
} else {
return x;
} }
} }
return x;
}
// Combinators. // Combinators.
function PromiseCast(x) { function PromiseCast(x) {
// TODO(rossberg): cannot do better until we support @@create. // TODO(rossberg): cannot do better until we support @@create.
return IsPromise(x) ? x : this.resolved(x); return IsPromise(x) ? x : this.resolve(x);
} }
function PromiseAll(values) { function PromiseAll(values) {
var deferred = this.deferred(); var deferred = %_CallFunction(this, PromiseDeferred);
var resolutions = []; var resolutions = [];
try {
var count = values.length; var count = values.length;
if (count === 0) { if (count === 0) {
deferred.resolve(resolutions); deferred.resolve(resolutions);
@ -261,24 +267,28 @@ function PromiseAll(values) {
resolutions[i] = x; resolutions[i] = x;
if (--count === 0) deferred.resolve(resolutions); if (--count === 0) deferred.resolve(resolutions);
}.bind(UNDEFINED, i), // TODO(rossberg): use let loop once available }.bind(UNDEFINED, i), // TODO(rossberg): use let loop once available
function(r) { function(r) { deferred.reject(r) }
if (count > 0) { count = 0; deferred.reject(r) }
}
); );
} }
} }
} catch (e) {
deferred.reject(e)
}
return deferred.promise; return deferred.promise;
} }
function PromiseOne(values) { // a.k.a. race function PromiseOne(values) {
var deferred = this.deferred(); var deferred = %_CallFunction(this, PromiseDeferred);
var done = false; try {
for (var i = 0; i < values.length; ++i) { for (var i = 0; i < values.length; ++i) {
this.cast(values[i]).chain( this.cast(values[i]).chain(
function(x) { if (!done) { done = true; deferred.resolve(x) } }, function(x) { deferred.resolve(x) },
function(r) { if (!done) { done = true; deferred.reject(r) } } function(r) { deferred.reject(r) }
); );
} }
} catch (e) {
deferred.reject(e)
}
return deferred.promise; return deferred.promise;
} }
@ -288,11 +298,11 @@ function SetUpPromise() {
%CheckIsBootstrapping() %CheckIsBootstrapping()
global.Promise = $Promise; global.Promise = $Promise;
InstallFunctions($Promise, DONT_ENUM, [ InstallFunctions($Promise, DONT_ENUM, [
"deferred", PromiseDeferred, "defer", PromiseDeferred,
"resolved", PromiseResolved, "resolve", PromiseResolved,
"rejected", PromiseRejected, "reject", PromiseRejected,
"all", PromiseAll, "all", PromiseAll,
"one", PromiseOne, "race", PromiseOne,
"cast", PromiseCast "cast", PromiseCast
]); ]);
InstallFunctions($Promise.prototype, DONT_ENUM, [ InstallFunctions($Promise.prototype, DONT_ENUM, [

View File

@ -2663,14 +2663,6 @@ RUNTIME_FUNCTION(MaybeObject*, Runtime_SpecialArrayFunctions) {
} }
RUNTIME_FUNCTION(MaybeObject*, Runtime_IsCallable) {
SealHandleScope shs(isolate);
ASSERT(args.length() == 1);
CONVERT_ARG_CHECKED(Object, obj, 0);
return isolate->heap()->ToBoolean(obj->IsCallable());
}
RUNTIME_FUNCTION(MaybeObject*, Runtime_IsClassicModeFunction) { RUNTIME_FUNCTION(MaybeObject*, Runtime_IsClassicModeFunction) {
SealHandleScope shs(isolate); SealHandleScope shs(isolate);
ASSERT(args.length() == 1); ASSERT(args.length() == 1);

View File

@ -64,7 +64,6 @@ namespace internal {
F(ToFastProperties, 1, 1) \ F(ToFastProperties, 1, 1) \
F(FinishArrayPrototypeSetup, 1, 1) \ F(FinishArrayPrototypeSetup, 1, 1) \
F(SpecialArrayFunctions, 1, 1) \ F(SpecialArrayFunctions, 1, 1) \
F(IsCallable, 1, 1) \
F(IsClassicModeFunction, 1, 1) \ F(IsClassicModeFunction, 1, 1) \
F(GetDefaultReceiver, 1, 1) \ F(GetDefaultReceiver, 1, 1) \
\ \

View File

@ -57,17 +57,33 @@ function assertAsyncDone(iteration) {
} }
(function() {
assertThrows(function() { Promise(function() {}) }, TypeError)
})();
(function() {
assertTrue(new Promise(function() {}) instanceof Promise)
})();
(function() { (function() {
assertThrows(function() { new Promise(5) }, TypeError) assertThrows(function() { new Promise(5) }, TypeError)
})(); })();
(function() { (function() {
assertThrows(function() { new Promise(function() { throw 5 }) }, 5) assertDoesNotThrow(function() { new Promise(function() { throw 5 }) })
})(); })();
(function() { (function() {
Promise.resolved(5); (new Promise(function() { throw 5 })).chain(
Promise.resolved(5).chain(undefined, assertUnreachable).chain( assertUnreachable,
function(r) { assertAsync(r === 5, "new-throw") }
)
assertAsyncRan()
})();
(function() {
Promise.resolve(5);
Promise.resolve(5).chain(undefined, assertUnreachable).chain(
function(x) { assertAsync(x === 5, "resolved/chain-nohandler") }, function(x) { assertAsync(x === 5, "resolved/chain-nohandler") },
assertUnreachable assertUnreachable
) )
@ -75,7 +91,7 @@ function assertAsyncDone(iteration) {
})(); })();
(function() { (function() {
Promise.rejected(5).chain(assertUnreachable, undefined).chain( Promise.reject(5).chain(assertUnreachable, undefined).chain(
assertUnreachable, assertUnreachable,
function(r) { assertAsync(r === 5, "rejected/chain-nohandler") } function(r) { assertAsync(r === 5, "rejected/chain-nohandler") }
) )
@ -83,7 +99,7 @@ function assertAsyncDone(iteration) {
})(); })();
(function() { (function() {
Promise.resolved(5).then(undefined, assertUnreachable).chain( Promise.resolve(5).then(undefined, assertUnreachable).chain(
function(x) { assertAsync(x === 5, "resolved/then-nohandler") }, function(x) { assertAsync(x === 5, "resolved/then-nohandler") },
assertUnreachable assertUnreachable
) )
@ -91,7 +107,7 @@ function assertAsyncDone(iteration) {
})(); })();
(function() { (function() {
Promise.rejected(5).then(assertUnreachable, undefined).chain( Promise.reject(5).then(assertUnreachable, undefined).chain(
assertUnreachable, assertUnreachable,
function(r) { assertAsync(r === 5, "rejected/then-nohandler") } function(r) { assertAsync(r === 5, "rejected/then-nohandler") }
) )
@ -99,9 +115,9 @@ function assertAsyncDone(iteration) {
})(); })();
(function() { (function() {
var p1 = Promise.resolved(5) var p1 = Promise.resolve(5)
var p2 = Promise.resolved(p1) var p2 = Promise.resolve(p1)
var p3 = Promise.resolved(p2) var p3 = Promise.resolve(p2)
p3.chain( p3.chain(
function(x) { assertAsync(x === p2, "resolved/chain") }, function(x) { assertAsync(x === p2, "resolved/chain") },
assertUnreachable assertUnreachable
@ -110,9 +126,9 @@ function assertAsyncDone(iteration) {
})(); })();
(function() { (function() {
var p1 = Promise.resolved(5) var p1 = Promise.resolve(5)
var p2 = Promise.resolved(p1) var p2 = Promise.resolve(p1)
var p3 = Promise.resolved(p2) var p3 = Promise.resolve(p2)
p3.then( p3.then(
function(x) { assertAsync(x === 5, "resolved/then") }, function(x) { assertAsync(x === 5, "resolved/then") },
assertUnreachable assertUnreachable
@ -121,9 +137,9 @@ function assertAsyncDone(iteration) {
})(); })();
(function() { (function() {
var p1 = Promise.rejected(5) var p1 = Promise.reject(5)
var p2 = Promise.resolved(p1) var p2 = Promise.resolve(p1)
var p3 = Promise.resolved(p2) var p3 = Promise.resolve(p2)
p3.chain( p3.chain(
function(x) { assertAsync(x === p2, "rejected/chain") }, function(x) { assertAsync(x === p2, "rejected/chain") },
assertUnreachable assertUnreachable
@ -132,9 +148,9 @@ function assertAsyncDone(iteration) {
})(); })();
(function() { (function() {
var p1 = Promise.rejected(5) var p1 = Promise.reject(5)
var p2 = Promise.resolved(p1) var p2 = Promise.resolve(p1)
var p3 = Promise.resolved(p2) var p3 = Promise.resolve(p2)
p3.then( p3.then(
assertUnreachable, assertUnreachable,
function(x) { assertAsync(x === 5, "rejected/then") } function(x) { assertAsync(x === 5, "rejected/then") }
@ -143,9 +159,9 @@ function assertAsyncDone(iteration) {
})(); })();
(function() { (function() {
var p1 = Promise.resolved(5) var p1 = Promise.resolve(5)
var p2 = Promise.resolved(p1) var p2 = Promise.resolve(p1)
var p3 = Promise.resolved(p2) var p3 = Promise.resolve(p2)
p3.chain(function(x) { return x }, assertUnreachable).chain( p3.chain(function(x) { return x }, assertUnreachable).chain(
function(x) { assertAsync(x === p1, "resolved/chain/chain") }, function(x) { assertAsync(x === p1, "resolved/chain/chain") },
assertUnreachable assertUnreachable
@ -154,9 +170,9 @@ function assertAsyncDone(iteration) {
})(); })();
(function() { (function() {
var p1 = Promise.resolved(5) var p1 = Promise.resolve(5)
var p2 = Promise.resolved(p1) var p2 = Promise.resolve(p1)
var p3 = Promise.resolved(p2) var p3 = Promise.resolve(p2)
p3.chain(function(x) { return x }, assertUnreachable).then( p3.chain(function(x) { return x }, assertUnreachable).then(
function(x) { assertAsync(x === 5, "resolved/chain/then") }, function(x) { assertAsync(x === 5, "resolved/chain/then") },
assertUnreachable assertUnreachable
@ -165,9 +181,9 @@ function assertAsyncDone(iteration) {
})(); })();
(function() { (function() {
var p1 = Promise.resolved(5) var p1 = Promise.resolve(5)
var p2 = Promise.resolved(p1) var p2 = Promise.resolve(p1)
var p3 = Promise.resolved(p2) var p3 = Promise.resolve(p2)
p3.chain(function(x) { return 6 }, assertUnreachable).chain( p3.chain(function(x) { return 6 }, assertUnreachable).chain(
function(x) { assertAsync(x === 6, "resolved/chain/chain2") }, function(x) { assertAsync(x === 6, "resolved/chain/chain2") },
assertUnreachable assertUnreachable
@ -176,9 +192,9 @@ function assertAsyncDone(iteration) {
})(); })();
(function() { (function() {
var p1 = Promise.resolved(5) var p1 = Promise.resolve(5)
var p2 = Promise.resolved(p1) var p2 = Promise.resolve(p1)
var p3 = Promise.resolved(p2) var p3 = Promise.resolve(p2)
p3.chain(function(x) { return 6 }, assertUnreachable).then( p3.chain(function(x) { return 6 }, assertUnreachable).then(
function(x) { assertAsync(x === 6, "resolved/chain/then2") }, function(x) { assertAsync(x === 6, "resolved/chain/then2") },
assertUnreachable assertUnreachable
@ -187,9 +203,9 @@ function assertAsyncDone(iteration) {
})(); })();
(function() { (function() {
var p1 = Promise.resolved(5) var p1 = Promise.resolve(5)
var p2 = Promise.resolved(p1) var p2 = Promise.resolve(p1)
var p3 = Promise.resolved(p2) var p3 = Promise.resolve(p2)
p3.then(function(x) { return x + 1 }, assertUnreachable).chain( p3.then(function(x) { return x + 1 }, assertUnreachable).chain(
function(x) { assertAsync(x === 6, "resolved/then/chain") }, function(x) { assertAsync(x === 6, "resolved/then/chain") },
assertUnreachable assertUnreachable
@ -198,9 +214,9 @@ function assertAsyncDone(iteration) {
})(); })();
(function() { (function() {
var p1 = Promise.resolved(5) var p1 = Promise.resolve(5)
var p2 = Promise.resolved(p1) var p2 = Promise.resolve(p1)
var p3 = Promise.resolved(p2) var p3 = Promise.resolve(p2)
p3.then(function(x) { return x + 1 }, assertUnreachable).then( p3.then(function(x) { return x + 1 }, assertUnreachable).then(
function(x) { assertAsync(x === 6, "resolved/then/then") }, function(x) { assertAsync(x === 6, "resolved/then/then") },
assertUnreachable assertUnreachable
@ -209,10 +225,10 @@ function assertAsyncDone(iteration) {
})(); })();
(function() { (function() {
var p1 = Promise.resolved(5) var p1 = Promise.resolve(5)
var p2 = Promise.resolved(p1) var p2 = Promise.resolve(p1)
var p3 = Promise.resolved(p2) var p3 = Promise.resolve(p2)
p3.then(function(x){ return Promise.resolved(x+1) }, assertUnreachable).chain( p3.then(function(x){ return Promise.resolve(x+1) }, assertUnreachable).chain(
function(x) { assertAsync(x === 6, "resolved/then/chain2") }, function(x) { assertAsync(x === 6, "resolved/then/chain2") },
assertUnreachable assertUnreachable
) )
@ -220,10 +236,10 @@ function assertAsyncDone(iteration) {
})(); })();
(function() { (function() {
var p1 = Promise.resolved(5) var p1 = Promise.resolve(5)
var p2 = Promise.resolved(p1) var p2 = Promise.resolve(p1)
var p3 = Promise.resolved(p2) var p3 = Promise.resolve(p2)
p3.then(function(x) { return Promise.resolved(x+1) }, assertUnreachable).then( p3.then(function(x) { return Promise.resolve(x+1) }, assertUnreachable).then(
function(x) { assertAsync(x === 6, "resolved/then/then2") }, function(x) { assertAsync(x === 6, "resolved/then/then2") },
assertUnreachable assertUnreachable
) )
@ -231,9 +247,9 @@ function assertAsyncDone(iteration) {
})(); })();
(function() { (function() {
var p1 = Promise.resolved(5) var p1 = Promise.resolve(5)
var p2 = Promise.resolved(p1) var p2 = Promise.resolve(p1)
var p3 = Promise.resolved(p2) var p3 = Promise.resolve(p2)
p3.chain(function(x) { throw 6 }, assertUnreachable).chain( p3.chain(function(x) { throw 6 }, assertUnreachable).chain(
assertUnreachable, assertUnreachable,
function(x) { assertAsync(x === 6, "resolved/chain-throw/chain") } function(x) { assertAsync(x === 6, "resolved/chain-throw/chain") }
@ -242,9 +258,9 @@ function assertAsyncDone(iteration) {
})(); })();
(function() { (function() {
var p1 = Promise.resolved(5) var p1 = Promise.resolve(5)
var p2 = Promise.resolved(p1) var p2 = Promise.resolve(p1)
var p3 = Promise.resolved(p2) var p3 = Promise.resolve(p2)
p3.chain(function(x) { throw 6 }, assertUnreachable).then( p3.chain(function(x) { throw 6 }, assertUnreachable).then(
assertUnreachable, assertUnreachable,
function(x) { assertAsync(x === 6, "resolved/chain-throw/then") } function(x) { assertAsync(x === 6, "resolved/chain-throw/then") }
@ -253,9 +269,9 @@ function assertAsyncDone(iteration) {
})(); })();
(function() { (function() {
var p1 = Promise.resolved(5) var p1 = Promise.resolve(5)
var p2 = Promise.resolved(p1) var p2 = Promise.resolve(p1)
var p3 = Promise.resolved(p2) var p3 = Promise.resolve(p2)
p3.then(function(x) { throw 6 }, assertUnreachable).chain( p3.then(function(x) { throw 6 }, assertUnreachable).chain(
assertUnreachable, assertUnreachable,
function(x) { assertAsync(x === 6, "resolved/then-throw/chain") } function(x) { assertAsync(x === 6, "resolved/then-throw/chain") }
@ -264,9 +280,9 @@ function assertAsyncDone(iteration) {
})(); })();
(function() { (function() {
var p1 = Promise.resolved(5) var p1 = Promise.resolve(5)
var p2 = Promise.resolved(p1) var p2 = Promise.resolve(p1)
var p3 = Promise.resolved(p2) var p3 = Promise.resolve(p2)
p3.then(function(x) { throw 6 }, assertUnreachable).then( p3.then(function(x) { throw 6 }, assertUnreachable).then(
assertUnreachable, assertUnreachable,
function(x) { assertAsync(x === 6, "resolved/then-throw/then") } function(x) { assertAsync(x === 6, "resolved/then-throw/then") }
@ -275,9 +291,9 @@ function assertAsyncDone(iteration) {
})(); })();
(function() { (function() {
var p1 = Promise.resolved(5) var p1 = Promise.resolve(5)
var p2 = {then: function(onResolve, onReject) { onResolve(p1) }} var p2 = {then: function(onResolve, onReject) { onResolve(p1) }}
var p3 = Promise.resolved(p2) var p3 = Promise.resolve(p2)
p3.chain( p3.chain(
function(x) { assertAsync(x === p2, "resolved/thenable/chain") }, function(x) { assertAsync(x === p2, "resolved/thenable/chain") },
assertUnreachable assertUnreachable
@ -286,9 +302,9 @@ function assertAsyncDone(iteration) {
})(); })();
(function() { (function() {
var p1 = Promise.resolved(5) var p1 = Promise.resolve(5)
var p2 = {then: function(onResolve, onReject) { onResolve(p1) }} var p2 = {then: function(onResolve, onReject) { onResolve(p1) }}
var p3 = Promise.resolved(p2) var p3 = Promise.resolve(p2)
p3.then( p3.then(
function(x) { assertAsync(x === 5, "resolved/thenable/then") }, function(x) { assertAsync(x === 5, "resolved/thenable/then") },
assertUnreachable assertUnreachable
@ -297,9 +313,9 @@ function assertAsyncDone(iteration) {
})(); })();
(function() { (function() {
var p1 = Promise.rejected(5) var p1 = Promise.reject(5)
var p2 = {then: function(onResolve, onReject) { onResolve(p1) }} var p2 = {then: function(onResolve, onReject) { onResolve(p1) }}
var p3 = Promise.resolved(p2) var p3 = Promise.resolve(p2)
p3.chain( p3.chain(
function(x) { assertAsync(x === p2, "rejected/thenable/chain") }, function(x) { assertAsync(x === p2, "rejected/thenable/chain") },
assertUnreachable assertUnreachable
@ -308,9 +324,9 @@ function assertAsyncDone(iteration) {
})(); })();
(function() { (function() {
var p1 = Promise.rejected(5) var p1 = Promise.reject(5)
var p2 = {then: function(onResolve, onReject) { onResolve(p1) }} var p2 = {then: function(onResolve, onReject) { onResolve(p1) }}
var p3 = Promise.resolved(p2) var p3 = Promise.resolve(p2)
p3.then( p3.then(
assertUnreachable, assertUnreachable,
function(x) { assertAsync(x === 5, "rejected/thenable/then") } function(x) { assertAsync(x === 5, "rejected/thenable/then") }
@ -319,10 +335,10 @@ function assertAsyncDone(iteration) {
})(); })();
(function() { (function() {
var deferred = Promise.deferred() var deferred = Promise.defer()
var p1 = deferred.promise var p1 = deferred.promise
var p2 = Promise.resolved(p1) var p2 = Promise.resolve(p1)
var p3 = Promise.resolved(p2) var p3 = Promise.resolve(p2)
p3.chain( p3.chain(
function(x) { assertAsync(x === p2, "chain/resolve") }, function(x) { assertAsync(x === p2, "chain/resolve") },
assertUnreachable assertUnreachable
@ -332,10 +348,10 @@ function assertAsyncDone(iteration) {
})(); })();
(function() { (function() {
var deferred = Promise.deferred() var deferred = Promise.defer()
var p1 = deferred.promise var p1 = deferred.promise
var p2 = Promise.resolved(p1) var p2 = Promise.resolve(p1)
var p3 = Promise.resolved(p2) var p3 = Promise.resolve(p2)
p3.then( p3.then(
function(x) { assertAsync(x === 5, "then/resolve") }, function(x) { assertAsync(x === 5, "then/resolve") },
assertUnreachable assertUnreachable
@ -345,10 +361,10 @@ function assertAsyncDone(iteration) {
})(); })();
(function() { (function() {
var deferred = Promise.deferred() var deferred = Promise.defer()
var p1 = deferred.promise var p1 = deferred.promise
var p2 = Promise.resolved(p1) var p2 = Promise.resolve(p1)
var p3 = Promise.resolved(p2) var p3 = Promise.resolve(p2)
p3.chain( p3.chain(
function(x) { assertAsync(x === p2, "chain/reject") }, function(x) { assertAsync(x === p2, "chain/reject") },
assertUnreachable assertUnreachable
@ -358,10 +374,10 @@ function assertAsyncDone(iteration) {
})(); })();
(function() { (function() {
var deferred = Promise.deferred() var deferred = Promise.defer()
var p1 = deferred.promise var p1 = deferred.promise
var p2 = Promise.resolved(p1) var p2 = Promise.resolve(p1)
var p3 = Promise.resolved(p2) var p3 = Promise.resolve(p2)
p3.then( p3.then(
assertUnreachable, assertUnreachable,
function(x) { assertAsync(x === 5, "then/reject") } function(x) { assertAsync(x === 5, "then/reject") }
@ -371,10 +387,10 @@ function assertAsyncDone(iteration) {
})(); })();
(function() { (function() {
var deferred = Promise.deferred() var deferred = Promise.defer()
var p1 = deferred.promise var p1 = deferred.promise
var p2 = {then: function(onResolve, onReject) { onResolve(p1) }} var p2 = {then: function(onResolve, onReject) { onResolve(p1) }}
var p3 = Promise.resolved(p2) var p3 = Promise.resolve(p2)
p3.chain( p3.chain(
function(x) { assertAsync(x === p2, "chain/resolve/thenable") }, function(x) { assertAsync(x === p2, "chain/resolve/thenable") },
assertUnreachable assertUnreachable
@ -384,10 +400,10 @@ function assertAsyncDone(iteration) {
})(); })();
(function() { (function() {
var deferred = Promise.deferred() var deferred = Promise.defer()
var p1 = deferred.promise var p1 = deferred.promise
var p2 = {then: function(onResolve, onReject) { onResolve(p1) }} var p2 = {then: function(onResolve, onReject) { onResolve(p1) }}
var p3 = Promise.resolved(p2) var p3 = Promise.resolve(p2)
p3.then( p3.then(
function(x) { assertAsync(x === 5, "then/resolve/thenable") }, function(x) { assertAsync(x === 5, "then/resolve/thenable") },
assertUnreachable assertUnreachable
@ -397,10 +413,10 @@ function assertAsyncDone(iteration) {
})(); })();
(function() { (function() {
var deferred = Promise.deferred() var deferred = Promise.defer()
var p1 = deferred.promise var p1 = deferred.promise
var p2 = {then: function(onResolve, onReject) { onResolve(p1) }} var p2 = {then: function(onResolve, onReject) { onResolve(p1) }}
var p3 = Promise.resolved(p2) var p3 = Promise.resolve(p2)
p3.chain( p3.chain(
function(x) { assertAsync(x === p2, "chain/reject/thenable") }, function(x) { assertAsync(x === p2, "chain/reject/thenable") },
assertUnreachable assertUnreachable
@ -410,10 +426,10 @@ function assertAsyncDone(iteration) {
})(); })();
(function() { (function() {
var deferred = Promise.deferred() var deferred = Promise.defer()
var p1 = deferred.promise var p1 = deferred.promise
var p2 = {then: function(onResolve, onReject) { onResolve(p1) }} var p2 = {then: function(onResolve, onReject) { onResolve(p1) }}
var p3 = Promise.resolved(p2) var p3 = Promise.resolve(p2)
p3.then( p3.then(
assertUnreachable, assertUnreachable,
function(x) { assertAsync(x === 5, "then/reject/thenable") } function(x) { assertAsync(x === 5, "then/reject/thenable") }
@ -423,9 +439,9 @@ function assertAsyncDone(iteration) {
})(); })();
(function() { (function() {
var p1 = Promise.resolved(5) var p1 = Promise.resolve(5)
var p2 = Promise.resolved(p1) var p2 = Promise.resolve(p1)
var deferred = Promise.deferred() var deferred = Promise.defer()
var p3 = deferred.promise var p3 = deferred.promise
p3.chain( p3.chain(
function(x) { assertAsync(x === p2, "chain/resolve2") }, function(x) { assertAsync(x === p2, "chain/resolve2") },
@ -436,9 +452,9 @@ function assertAsyncDone(iteration) {
})(); })();
(function() { (function() {
var p1 = Promise.resolved(5) var p1 = Promise.resolve(5)
var p2 = Promise.resolved(p1) var p2 = Promise.resolve(p1)
var deferred = Promise.deferred() var deferred = Promise.defer()
var p3 = deferred.promise var p3 = deferred.promise
p3.then( p3.then(
function(x) { assertAsync(x === 5, "then/resolve2") }, function(x) { assertAsync(x === 5, "then/resolve2") },
@ -449,9 +465,9 @@ function assertAsyncDone(iteration) {
})(); })();
(function() { (function() {
var p1 = Promise.resolved(5) var p1 = Promise.resolve(5)
var p2 = Promise.resolved(p1) var p2 = Promise.resolve(p1)
var deferred = Promise.deferred() var deferred = Promise.defer()
var p3 = deferred.promise var p3 = deferred.promise
p3.chain( p3.chain(
assertUnreachable, assertUnreachable,
@ -462,9 +478,9 @@ function assertAsyncDone(iteration) {
})(); })();
(function() { (function() {
var p1 = Promise.resolved(5) var p1 = Promise.resolve(5)
var p2 = Promise.resolved(p1) var p2 = Promise.resolve(p1)
var deferred = Promise.deferred() var deferred = Promise.defer()
var p3 = deferred.promise var p3 = deferred.promise
p3.then( p3.then(
assertUnreachable, assertUnreachable,
@ -475,9 +491,9 @@ function assertAsyncDone(iteration) {
})(); })();
(function() { (function() {
var p1 = Promise.resolved(5) var p1 = Promise.resolve(5)
var p2 = {then: function(onResolve, onReject) { onResolve(p1) }} var p2 = {then: function(onResolve, onReject) { onResolve(p1) }}
var deferred = Promise.deferred() var deferred = Promise.defer()
var p3 = deferred.promise var p3 = deferred.promise
p3.chain( p3.chain(
function(x) { assertAsync(x === p2, "chain/resolve/thenable2") }, function(x) { assertAsync(x === p2, "chain/resolve/thenable2") },
@ -488,9 +504,9 @@ function assertAsyncDone(iteration) {
})(); })();
(function() { (function() {
var p1 = Promise.resolved(5) var p1 = Promise.resolve(5)
var p2 = {then: function(onResolve, onReject) { onResolve(p1) }} var p2 = {then: function(onResolve, onReject) { onResolve(p1) }}
var deferred = Promise.deferred() var deferred = Promise.defer()
var p3 = deferred.promise var p3 = deferred.promise
p3.then( p3.then(
function(x) { assertAsync(x === 5, "then/resolve/thenable2") }, function(x) { assertAsync(x === 5, "then/resolve/thenable2") },
@ -501,7 +517,7 @@ function assertAsyncDone(iteration) {
})(); })();
(function() { (function() {
var p1 = Promise.resolved(0) var p1 = Promise.resolve(0)
var p2 = p1.chain(function(x) { return p2 }, assertUnreachable) var p2 = p1.chain(function(x) { return p2 }, assertUnreachable)
p2.chain( p2.chain(
assertUnreachable, assertUnreachable,
@ -511,7 +527,7 @@ function assertAsyncDone(iteration) {
})(); })();
(function() { (function() {
var p1 = Promise.resolved(0) var p1 = Promise.resolve(0)
var p2 = p1.then(function(x) { return p2 }, assertUnreachable) var p2 = p1.then(function(x) { return p2 }, assertUnreachable)
p2.chain( p2.chain(
assertUnreachable, assertUnreachable,
@ -521,7 +537,7 @@ function assertAsyncDone(iteration) {
})(); })();
(function() { (function() {
var deferred = Promise.deferred() var deferred = Promise.defer()
var p = deferred.promise var p = deferred.promise
deferred.resolve(p) deferred.resolve(p)
p.chain( p.chain(
@ -532,7 +548,7 @@ function assertAsyncDone(iteration) {
})(); })();
(function() { (function() {
var deferred = Promise.deferred() var deferred = Promise.defer()
var p = deferred.promise var p = deferred.promise
deferred.resolve(p) deferred.resolve(p)
p.then( p.then(
@ -542,6 +558,14 @@ function assertAsyncDone(iteration) {
assertAsyncRan() assertAsyncRan()
})(); })();
(function() {
Promise.all({get length() { throw 666 }}).chain(
assertUnreachable,
function(r) { assertAsync(r === 666, "all/no-array") }
)
assertAsyncRan()
})();
(function() { (function() {
Promise.all([]).chain( Promise.all([]).chain(
function(x) { assertAsync(x.length === 0, "all/resolve/empty") }, function(x) { assertAsync(x.length === 0, "all/resolve/empty") },
@ -551,11 +575,11 @@ function assertAsyncDone(iteration) {
})(); })();
(function() { (function() {
var deferred1 = Promise.deferred() var deferred1 = Promise.defer()
var p1 = deferred1.promise var p1 = deferred1.promise
var deferred2 = Promise.deferred() var deferred2 = Promise.defer()
var p2 = deferred2.promise var p2 = deferred2.promise
var deferred3 = Promise.deferred() var deferred3 = Promise.defer()
var p3 = deferred3.promise var p3 = deferred3.promise
Promise.all([p1, p2, p3]).chain( Promise.all([p1, p2, p3]).chain(
function(x) { function(x) {
@ -576,10 +600,10 @@ function assertAsyncDone(iteration) {
})(); })();
(function() { (function() {
var deferred = Promise.deferred() var deferred = Promise.defer()
var p1 = deferred.promise var p1 = deferred.promise
var p2 = Promise.resolved(2) var p2 = Promise.resolve(2)
var p3 = Promise.deferred().promise var p3 = Promise.defer().promise
Promise.all([p1, p2, p3]).chain( Promise.all([p1, p2, p3]).chain(
assertUnreachable, assertUnreachable,
assertUnreachable assertUnreachable
@ -588,11 +612,11 @@ function assertAsyncDone(iteration) {
})(); })();
(function() { (function() {
var deferred1 = Promise.deferred() var deferred1 = Promise.defer()
var p1 = deferred1.promise var p1 = deferred1.promise
var deferred2 = Promise.deferred() var deferred2 = Promise.defer()
var p2 = deferred2.promise var p2 = deferred2.promise
var deferred3 = Promise.deferred() var deferred3 = Promise.defer()
var p3 = deferred3.promise var p3 = deferred3.promise
Promise.all([p1, p2, p3]).chain( Promise.all([p1, p2, p3]).chain(
assertUnreachable, assertUnreachable,
@ -605,42 +629,50 @@ function assertAsyncDone(iteration) {
})(); })();
(function() { (function() {
Promise.one([]).chain( Promise.race([]).chain(
assertUnreachable, assertUnreachable,
assertUnreachable assertUnreachable
) )
})(); })();
(function() { (function() {
var p1 = Promise.resolved(1) var p1 = Promise.resolve(1)
var p2 = Promise.resolved(2) var p2 = Promise.resolve(2)
var p3 = Promise.resolved(3) var p3 = Promise.resolve(3)
Promise.one([p1, p2, p3]).chain( Promise.race([p1, p2, p3]).chain(
function(x) { assertAsync(x === 1, "resolved/all") }, function(x) { assertAsync(x === 1, "resolved/one") },
assertUnreachable assertUnreachable
) )
assertAsyncRan() assertAsyncRan()
})(); })();
(function() { (function() {
var p1 = Promise.resolved(1) var p1 = Promise.resolve(1)
var p2 = Promise.resolved(2) var p2 = Promise.resolve(2)
var p3 = Promise.resolved(3) var p3 = Promise.resolve(3)
Promise.one([0, p1, p2, p3]).chain( Promise.race([0, p1, p2, p3]).chain(
function(x) { assertAsync(x === 0, "resolved-const/all") }, function(x) { assertAsync(x === 0, "resolved-const/one") },
assertUnreachable assertUnreachable
) )
assertAsyncRan() assertAsyncRan()
})(); })();
(function() { (function() {
var deferred1 = Promise.deferred() Promise.race({get length() { throw 666 }}).chain(
assertUnreachable,
function(r) { assertAsync(r === 666, "one/no-array") }
)
assertAsyncRan()
})();
(function() {
var deferred1 = Promise.defer()
var p1 = deferred1.promise var p1 = deferred1.promise
var deferred2 = Promise.deferred() var deferred2 = Promise.defer()
var p2 = deferred2.promise var p2 = deferred2.promise
var deferred3 = Promise.deferred() var deferred3 = Promise.defer()
var p3 = deferred3.promise var p3 = deferred3.promise
Promise.one([p1, p2, p3]).chain( Promise.race([p1, p2, p3]).chain(
function(x) { assertAsync(x === 3, "one/resolve") }, function(x) { assertAsync(x === 3, "one/resolve") },
assertUnreachable assertUnreachable
) )
@ -650,11 +682,11 @@ function assertAsyncDone(iteration) {
})(); })();
(function() { (function() {
var deferred = Promise.deferred() var deferred = Promise.defer()
var p1 = deferred.promise var p1 = deferred.promise
var p2 = Promise.resolved(2) var p2 = Promise.resolve(2)
var p3 = Promise.deferred().promise var p3 = Promise.defer().promise
Promise.one([p1, p2, p3]).chain( Promise.race([p1, p2, p3]).chain(
function(x) { assertAsync(x === 2, "resolved/one") }, function(x) { assertAsync(x === 2, "resolved/one") },
assertUnreachable assertUnreachable
) )
@ -663,13 +695,13 @@ function assertAsyncDone(iteration) {
})(); })();
(function() { (function() {
var deferred1 = Promise.deferred() var deferred1 = Promise.defer()
var p1 = deferred1.promise var p1 = deferred1.promise
var deferred2 = Promise.deferred() var deferred2 = Promise.defer()
var p2 = deferred2.promise var p2 = deferred2.promise
var deferred3 = Promise.deferred() var deferred3 = Promise.defer()
var p3 = deferred3.promise var p3 = deferred3.promise
Promise.one([p1, p2, p3]).chain( Promise.race([p1, p2, p3]).chain(
function(x) { assertAsync(x === 3, "one/resolve/reject") }, function(x) { assertAsync(x === 3, "one/resolve/reject") },
assertUnreachable assertUnreachable
) )
@ -679,13 +711,13 @@ function assertAsyncDone(iteration) {
})(); })();
(function() { (function() {
var deferred1 = Promise.deferred() var deferred1 = Promise.defer()
var p1 = deferred1.promise var p1 = deferred1.promise
var deferred2 = Promise.deferred() var deferred2 = Promise.defer()
var p2 = deferred2.promise var p2 = deferred2.promise
var deferred3 = Promise.deferred() var deferred3 = Promise.defer()
var p3 = deferred3.promise var p3 = deferred3.promise
Promise.one([p1, p2, p3]).chain( Promise.race([p1, p2, p3]).chain(
assertUnreachable, assertUnreachable,
function(x) { assertAsync(x === 3, "one/reject/resolve") } function(x) { assertAsync(x === 3, "one/reject/resolve") }
) )
@ -698,56 +730,61 @@ function assertAsyncDone(iteration) {
var log var log
function MyPromise(resolver) { function MyPromise(resolver) {
log += "n" log += "n"
Promise.call(this, var promise = new Promise(function(resolve, reject) {
function(resolve, reject) {
resolver( resolver(
function(x) { log += "x" + x; resolve(x) }, function(x) { log += "x" + x; resolve(x) },
function(r) { log += "r" + r; reject(r) } function(r) { log += "r" + r; reject(r) }
) )
} })
) promise.__proto__ = MyPromise.prototype
return promise
} }
MyPromise.__proto__ = Promise MyPromise.__proto__ = Promise
MyPromise.deferred = function() { MyPromise.defer = function() {
log += "d" log += "d"
return this.__proto__.deferred.call(this) return this.__proto__.defer.call(this)
} }
MyPromise.prototype.__proto__ = Promise.prototype MyPromise.prototype.__proto__ = Promise.prototype
MyPromise.prototype.chain = function(resolve, reject) { MyPromise.prototype.chain = function(resolve, reject) {
log += "w" log += "c"
return this.__proto__.__proto__.chain.call(this, resolve, reject) return this.__proto__.__proto__.chain.call(this, resolve, reject)
} }
log = "" log = ""
var p1 = new MyPromise(function(resolve, reject) { resolve(1) }) var p1 = new MyPromise(function(resolve, reject) { resolve(1) })
var p2 = new MyPromise(function(resolve, reject) { reject(2) }) var p2 = new MyPromise(function(resolve, reject) { reject(2) })
var d3 = MyPromise.deferred() var d3 = MyPromise.defer()
assertTrue(d3.promise instanceof MyPromise, "subclass/instance3") assertTrue(d3.promise instanceof Promise, "subclass/instance")
assertTrue(d3.promise instanceof MyPromise, "subclass/instance-my3")
assertTrue(log === "nx1nr2dn", "subclass/create") assertTrue(log === "nx1nr2dn", "subclass/create")
log = "" log = ""
var p4 = MyPromise.resolved(4) var p4 = MyPromise.resolve(4)
var p5 = MyPromise.rejected(5) var p5 = MyPromise.reject(5)
assertTrue(p4 instanceof MyPromise, "subclass/instance4") assertTrue(p4 instanceof Promise, "subclass/instance4")
assertTrue(p5 instanceof MyPromise, "subclass/instance5") assertTrue(p4 instanceof MyPromise, "subclass/instance-my4")
assertTrue(p5 instanceof Promise, "subclass/instance5")
assertTrue(p5 instanceof MyPromise, "subclass/instance-my5")
d3.resolve(3) d3.resolve(3)
assertTrue(log === "nx4nr5x3", "subclass/resolve") assertTrue(log === "nx4nr5x3", "subclass/resolve")
log = "" log = ""
var d6 = MyPromise.deferred() var d6 = MyPromise.defer()
d6.promise.chain(function(x) { return new Promise(x) }).chain(function() {}) d6.promise.chain(function(x) {
return new Promise(function(resolve) { resolve(x) })
}).chain(function() {})
d6.resolve(6) d6.resolve(6)
assertTrue(log === "dnwnwnx6", "subclass/chain") assertTrue(log === "dncncnx6", "subclass/chain")
log = "" log = ""
Promise.all([11, Promise.resolved(12), 13, MyPromise.resolved(14), 15, 16]) Promise.all([11, Promise.resolve(12), 13, MyPromise.resolve(14), 15, 16])
assertTrue(log === "nx14wn", "subclass/all/arg") assertTrue(log === "nx14cn", "subclass/all/arg")
log = "" log = ""
MyPromise.all([21, Promise.resolved(22), 23, MyPromise.resolved(24), 25, 26]) MyPromise.all([21, Promise.resolve(22), 23, MyPromise.resolve(24), 25, 26])
assertTrue(log === "nx24dnnx21wnnx23wnwnnx25wnnx26wn", "subclass/all/self") assertTrue(log === "nx24nnx21cnnx23cncnnx25cnnx26cn", "subclass/all/self")
})(); })();