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:
parent
61692bf294
commit
3286bc71e3
@ -109,8 +109,9 @@ var kMessages = {
|
||||
invalid_argument: ["invalid_argument"],
|
||||
data_view_not_array_buffer: ["First argument to DataView constructor must be an ArrayBuffer"],
|
||||
constructor_not_function: ["Constructor ", "%0", " requires 'new'"],
|
||||
not_a_promise: ["%0", "is not a promise"],
|
||||
promise_cyclic: ["Chaining cycle detected for promise", "%0"],
|
||||
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"],
|
||||
array_functions_on_frozen: ["Cannot modify frozen array elements"],
|
||||
array_functions_change_sealed: ["Cannot add/remove sealed array elements"],
|
||||
// RangeError
|
||||
|
112
src/promise.js
112
src/promise.js
@ -62,11 +62,16 @@ function IsPromise(x) {
|
||||
|
||||
function Promise(resolver) {
|
||||
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);
|
||||
resolver(function(x) { PromiseResolve(promise, x) },
|
||||
function(r) { PromiseReject(promise, r) });
|
||||
// TODO(rossberg): current draft makes exception from this call asynchronous,
|
||||
// but that's probably a mistake.
|
||||
try {
|
||||
resolver(function(x) { PromiseResolve(promise, x) },
|
||||
function(r) { PromiseReject(promise, r) });
|
||||
} catch (e) {
|
||||
PromiseReject(promise, e);
|
||||
}
|
||||
}
|
||||
|
||||
function PromiseSet(promise, status, value, onResolve, onReject) {
|
||||
@ -82,9 +87,10 @@ function PromiseInit(promise) {
|
||||
}
|
||||
|
||||
function PromiseDone(promise, status, value, promiseQueue) {
|
||||
if (GET_PRIVATE(promise, promiseStatus) !== 0) return;
|
||||
PromiseEnqueue(value, GET_PRIVATE(promise, promiseQueue));
|
||||
PromiseSet(promise, status, value);
|
||||
if (GET_PRIVATE(promise, promiseStatus) === 0) {
|
||||
PromiseEnqueue(value, GET_PRIVATE(promise, promiseQueue));
|
||||
PromiseSet(promise, status, value);
|
||||
}
|
||||
}
|
||||
|
||||
function PromiseResolve(promise, x) {
|
||||
@ -219,25 +225,24 @@ function PromiseThen(onResolve, onReject) {
|
||||
PromiseCoerce.table = new $WeakMap;
|
||||
|
||||
function PromiseCoerce(constructor, x) {
|
||||
var then;
|
||||
if (IsPromise(x)) {
|
||||
return x;
|
||||
} else if (!IS_NULL_OR_UNDEFINED(x) && %IsCallable(then = x.then)) {
|
||||
if (PromiseCoerce.table.has(x)) {
|
||||
return PromiseCoerce.table.get(x);
|
||||
} else {
|
||||
var deferred = constructor.deferred();
|
||||
PromiseCoerce.table.set(x, deferred.promise);
|
||||
try {
|
||||
%_CallFunction(x, deferred.resolve, deferred.reject, then);
|
||||
} catch(e) {
|
||||
deferred.reject(e);
|
||||
if (!(IsPromise(x) || IS_NULL_OR_UNDEFINED(x))) {
|
||||
var then = x.then;
|
||||
if (typeof then === 'function') {
|
||||
if (PromiseCoerce.table.has(x)) {
|
||||
return PromiseCoerce.table.get(x);
|
||||
} else {
|
||||
var deferred = %_CallFunction(constructor, PromiseDeferred);
|
||||
PromiseCoerce.table.set(x, deferred.promise);
|
||||
try {
|
||||
%_CallFunction(x, deferred.resolve, deferred.reject, then);
|
||||
} catch(e) {
|
||||
deferred.reject(e);
|
||||
}
|
||||
return deferred.promise;
|
||||
}
|
||||
return deferred.promise;
|
||||
}
|
||||
} else {
|
||||
return x;
|
||||
}
|
||||
return x;
|
||||
}
|
||||
|
||||
|
||||
@ -245,39 +250,44 @@ function PromiseCoerce(constructor, x) {
|
||||
|
||||
function PromiseCast(x) {
|
||||
// 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) {
|
||||
var deferred = this.deferred();
|
||||
var deferred = %_CallFunction(this, PromiseDeferred);
|
||||
var resolutions = [];
|
||||
var count = values.length;
|
||||
if (count === 0) {
|
||||
deferred.resolve(resolutions);
|
||||
} else {
|
||||
for (var i = 0; i < values.length; ++i) {
|
||||
this.cast(values[i]).chain(
|
||||
function(i, x) {
|
||||
resolutions[i] = x;
|
||||
if (--count === 0) deferred.resolve(resolutions);
|
||||
}.bind(UNDEFINED, i), // TODO(rossberg): use let loop once available
|
||||
function(r) {
|
||||
if (count > 0) { count = 0; deferred.reject(r) }
|
||||
}
|
||||
);
|
||||
try {
|
||||
var count = values.length;
|
||||
if (count === 0) {
|
||||
deferred.resolve(resolutions);
|
||||
} else {
|
||||
for (var i = 0; i < values.length; ++i) {
|
||||
this.cast(values[i]).chain(
|
||||
function(i, x) {
|
||||
resolutions[i] = x;
|
||||
if (--count === 0) deferred.resolve(resolutions);
|
||||
}.bind(UNDEFINED, i), // TODO(rossberg): use let loop once available
|
||||
function(r) { deferred.reject(r) }
|
||||
);
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
deferred.reject(e)
|
||||
}
|
||||
return deferred.promise;
|
||||
}
|
||||
|
||||
function PromiseOne(values) { // a.k.a. race
|
||||
var deferred = this.deferred();
|
||||
var done = false;
|
||||
for (var i = 0; i < values.length; ++i) {
|
||||
this.cast(values[i]).chain(
|
||||
function(x) { if (!done) { done = true; deferred.resolve(x) } },
|
||||
function(r) { if (!done) { done = true; deferred.reject(r) } }
|
||||
);
|
||||
function PromiseOne(values) {
|
||||
var deferred = %_CallFunction(this, PromiseDeferred);
|
||||
try {
|
||||
for (var i = 0; i < values.length; ++i) {
|
||||
this.cast(values[i]).chain(
|
||||
function(x) { deferred.resolve(x) },
|
||||
function(r) { deferred.reject(r) }
|
||||
);
|
||||
}
|
||||
} catch (e) {
|
||||
deferred.reject(e)
|
||||
}
|
||||
return deferred.promise;
|
||||
}
|
||||
@ -288,11 +298,11 @@ function SetUpPromise() {
|
||||
%CheckIsBootstrapping()
|
||||
global.Promise = $Promise;
|
||||
InstallFunctions($Promise, DONT_ENUM, [
|
||||
"deferred", PromiseDeferred,
|
||||
"resolved", PromiseResolved,
|
||||
"rejected", PromiseRejected,
|
||||
"defer", PromiseDeferred,
|
||||
"resolve", PromiseResolved,
|
||||
"reject", PromiseRejected,
|
||||
"all", PromiseAll,
|
||||
"one", PromiseOne,
|
||||
"race", PromiseOne,
|
||||
"cast", PromiseCast
|
||||
]);
|
||||
InstallFunctions($Promise.prototype, DONT_ENUM, [
|
||||
|
@ -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) {
|
||||
SealHandleScope shs(isolate);
|
||||
ASSERT(args.length() == 1);
|
||||
|
@ -64,7 +64,6 @@ namespace internal {
|
||||
F(ToFastProperties, 1, 1) \
|
||||
F(FinishArrayPrototypeSetup, 1, 1) \
|
||||
F(SpecialArrayFunctions, 1, 1) \
|
||||
F(IsCallable, 1, 1) \
|
||||
F(IsClassicModeFunction, 1, 1) \
|
||||
F(GetDefaultReceiver, 1, 1) \
|
||||
\
|
||||
|
@ -57,17 +57,33 @@ function assertAsyncDone(iteration) {
|
||||
}
|
||||
|
||||
|
||||
(function() {
|
||||
assertThrows(function() { Promise(function() {}) }, TypeError)
|
||||
})();
|
||||
|
||||
(function() {
|
||||
assertTrue(new Promise(function() {}) instanceof Promise)
|
||||
})();
|
||||
|
||||
(function() {
|
||||
assertThrows(function() { new Promise(5) }, TypeError)
|
||||
})();
|
||||
|
||||
(function() {
|
||||
assertThrows(function() { new Promise(function() { throw 5 }) }, 5)
|
||||
assertDoesNotThrow(function() { new Promise(function() { throw 5 }) })
|
||||
})();
|
||||
|
||||
(function() {
|
||||
Promise.resolved(5);
|
||||
Promise.resolved(5).chain(undefined, assertUnreachable).chain(
|
||||
(new Promise(function() { throw 5 })).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") },
|
||||
assertUnreachable
|
||||
)
|
||||
@ -75,7 +91,7 @@ function assertAsyncDone(iteration) {
|
||||
})();
|
||||
|
||||
(function() {
|
||||
Promise.rejected(5).chain(assertUnreachable, undefined).chain(
|
||||
Promise.reject(5).chain(assertUnreachable, undefined).chain(
|
||||
assertUnreachable,
|
||||
function(r) { assertAsync(r === 5, "rejected/chain-nohandler") }
|
||||
)
|
||||
@ -83,7 +99,7 @@ function assertAsyncDone(iteration) {
|
||||
})();
|
||||
|
||||
(function() {
|
||||
Promise.resolved(5).then(undefined, assertUnreachable).chain(
|
||||
Promise.resolve(5).then(undefined, assertUnreachable).chain(
|
||||
function(x) { assertAsync(x === 5, "resolved/then-nohandler") },
|
||||
assertUnreachable
|
||||
)
|
||||
@ -91,7 +107,7 @@ function assertAsyncDone(iteration) {
|
||||
})();
|
||||
|
||||
(function() {
|
||||
Promise.rejected(5).then(assertUnreachable, undefined).chain(
|
||||
Promise.reject(5).then(assertUnreachable, undefined).chain(
|
||||
assertUnreachable,
|
||||
function(r) { assertAsync(r === 5, "rejected/then-nohandler") }
|
||||
)
|
||||
@ -99,9 +115,9 @@ function assertAsyncDone(iteration) {
|
||||
})();
|
||||
|
||||
(function() {
|
||||
var p1 = Promise.resolved(5)
|
||||
var p2 = Promise.resolved(p1)
|
||||
var p3 = Promise.resolved(p2)
|
||||
var p1 = Promise.resolve(5)
|
||||
var p2 = Promise.resolve(p1)
|
||||
var p3 = Promise.resolve(p2)
|
||||
p3.chain(
|
||||
function(x) { assertAsync(x === p2, "resolved/chain") },
|
||||
assertUnreachable
|
||||
@ -110,9 +126,9 @@ function assertAsyncDone(iteration) {
|
||||
})();
|
||||
|
||||
(function() {
|
||||
var p1 = Promise.resolved(5)
|
||||
var p2 = Promise.resolved(p1)
|
||||
var p3 = Promise.resolved(p2)
|
||||
var p1 = Promise.resolve(5)
|
||||
var p2 = Promise.resolve(p1)
|
||||
var p3 = Promise.resolve(p2)
|
||||
p3.then(
|
||||
function(x) { assertAsync(x === 5, "resolved/then") },
|
||||
assertUnreachable
|
||||
@ -121,9 +137,9 @@ function assertAsyncDone(iteration) {
|
||||
})();
|
||||
|
||||
(function() {
|
||||
var p1 = Promise.rejected(5)
|
||||
var p2 = Promise.resolved(p1)
|
||||
var p3 = Promise.resolved(p2)
|
||||
var p1 = Promise.reject(5)
|
||||
var p2 = Promise.resolve(p1)
|
||||
var p3 = Promise.resolve(p2)
|
||||
p3.chain(
|
||||
function(x) { assertAsync(x === p2, "rejected/chain") },
|
||||
assertUnreachable
|
||||
@ -132,9 +148,9 @@ function assertAsyncDone(iteration) {
|
||||
})();
|
||||
|
||||
(function() {
|
||||
var p1 = Promise.rejected(5)
|
||||
var p2 = Promise.resolved(p1)
|
||||
var p3 = Promise.resolved(p2)
|
||||
var p1 = Promise.reject(5)
|
||||
var p2 = Promise.resolve(p1)
|
||||
var p3 = Promise.resolve(p2)
|
||||
p3.then(
|
||||
assertUnreachable,
|
||||
function(x) { assertAsync(x === 5, "rejected/then") }
|
||||
@ -143,9 +159,9 @@ function assertAsyncDone(iteration) {
|
||||
})();
|
||||
|
||||
(function() {
|
||||
var p1 = Promise.resolved(5)
|
||||
var p2 = Promise.resolved(p1)
|
||||
var p3 = Promise.resolved(p2)
|
||||
var p1 = Promise.resolve(5)
|
||||
var p2 = Promise.resolve(p1)
|
||||
var p3 = Promise.resolve(p2)
|
||||
p3.chain(function(x) { return x }, assertUnreachable).chain(
|
||||
function(x) { assertAsync(x === p1, "resolved/chain/chain") },
|
||||
assertUnreachable
|
||||
@ -154,9 +170,9 @@ function assertAsyncDone(iteration) {
|
||||
})();
|
||||
|
||||
(function() {
|
||||
var p1 = Promise.resolved(5)
|
||||
var p2 = Promise.resolved(p1)
|
||||
var p3 = Promise.resolved(p2)
|
||||
var p1 = Promise.resolve(5)
|
||||
var p2 = Promise.resolve(p1)
|
||||
var p3 = Promise.resolve(p2)
|
||||
p3.chain(function(x) { return x }, assertUnreachable).then(
|
||||
function(x) { assertAsync(x === 5, "resolved/chain/then") },
|
||||
assertUnreachable
|
||||
@ -165,9 +181,9 @@ function assertAsyncDone(iteration) {
|
||||
})();
|
||||
|
||||
(function() {
|
||||
var p1 = Promise.resolved(5)
|
||||
var p2 = Promise.resolved(p1)
|
||||
var p3 = Promise.resolved(p2)
|
||||
var p1 = Promise.resolve(5)
|
||||
var p2 = Promise.resolve(p1)
|
||||
var p3 = Promise.resolve(p2)
|
||||
p3.chain(function(x) { return 6 }, assertUnreachable).chain(
|
||||
function(x) { assertAsync(x === 6, "resolved/chain/chain2") },
|
||||
assertUnreachable
|
||||
@ -176,9 +192,9 @@ function assertAsyncDone(iteration) {
|
||||
})();
|
||||
|
||||
(function() {
|
||||
var p1 = Promise.resolved(5)
|
||||
var p2 = Promise.resolved(p1)
|
||||
var p3 = Promise.resolved(p2)
|
||||
var p1 = Promise.resolve(5)
|
||||
var p2 = Promise.resolve(p1)
|
||||
var p3 = Promise.resolve(p2)
|
||||
p3.chain(function(x) { return 6 }, assertUnreachable).then(
|
||||
function(x) { assertAsync(x === 6, "resolved/chain/then2") },
|
||||
assertUnreachable
|
||||
@ -187,9 +203,9 @@ function assertAsyncDone(iteration) {
|
||||
})();
|
||||
|
||||
(function() {
|
||||
var p1 = Promise.resolved(5)
|
||||
var p2 = Promise.resolved(p1)
|
||||
var p3 = Promise.resolved(p2)
|
||||
var p1 = Promise.resolve(5)
|
||||
var p2 = Promise.resolve(p1)
|
||||
var p3 = Promise.resolve(p2)
|
||||
p3.then(function(x) { return x + 1 }, assertUnreachable).chain(
|
||||
function(x) { assertAsync(x === 6, "resolved/then/chain") },
|
||||
assertUnreachable
|
||||
@ -198,9 +214,9 @@ function assertAsyncDone(iteration) {
|
||||
})();
|
||||
|
||||
(function() {
|
||||
var p1 = Promise.resolved(5)
|
||||
var p2 = Promise.resolved(p1)
|
||||
var p3 = Promise.resolved(p2)
|
||||
var p1 = Promise.resolve(5)
|
||||
var p2 = Promise.resolve(p1)
|
||||
var p3 = Promise.resolve(p2)
|
||||
p3.then(function(x) { return x + 1 }, assertUnreachable).then(
|
||||
function(x) { assertAsync(x === 6, "resolved/then/then") },
|
||||
assertUnreachable
|
||||
@ -209,10 +225,10 @@ function assertAsyncDone(iteration) {
|
||||
})();
|
||||
|
||||
(function() {
|
||||
var p1 = Promise.resolved(5)
|
||||
var p2 = Promise.resolved(p1)
|
||||
var p3 = Promise.resolved(p2)
|
||||
p3.then(function(x){ return Promise.resolved(x+1) }, assertUnreachable).chain(
|
||||
var p1 = Promise.resolve(5)
|
||||
var p2 = Promise.resolve(p1)
|
||||
var p3 = Promise.resolve(p2)
|
||||
p3.then(function(x){ return Promise.resolve(x+1) }, assertUnreachable).chain(
|
||||
function(x) { assertAsync(x === 6, "resolved/then/chain2") },
|
||||
assertUnreachable
|
||||
)
|
||||
@ -220,10 +236,10 @@ function assertAsyncDone(iteration) {
|
||||
})();
|
||||
|
||||
(function() {
|
||||
var p1 = Promise.resolved(5)
|
||||
var p2 = Promise.resolved(p1)
|
||||
var p3 = Promise.resolved(p2)
|
||||
p3.then(function(x) { return Promise.resolved(x+1) }, assertUnreachable).then(
|
||||
var p1 = Promise.resolve(5)
|
||||
var p2 = Promise.resolve(p1)
|
||||
var p3 = Promise.resolve(p2)
|
||||
p3.then(function(x) { return Promise.resolve(x+1) }, assertUnreachable).then(
|
||||
function(x) { assertAsync(x === 6, "resolved/then/then2") },
|
||||
assertUnreachable
|
||||
)
|
||||
@ -231,9 +247,9 @@ function assertAsyncDone(iteration) {
|
||||
})();
|
||||
|
||||
(function() {
|
||||
var p1 = Promise.resolved(5)
|
||||
var p2 = Promise.resolved(p1)
|
||||
var p3 = Promise.resolved(p2)
|
||||
var p1 = Promise.resolve(5)
|
||||
var p2 = Promise.resolve(p1)
|
||||
var p3 = Promise.resolve(p2)
|
||||
p3.chain(function(x) { throw 6 }, assertUnreachable).chain(
|
||||
assertUnreachable,
|
||||
function(x) { assertAsync(x === 6, "resolved/chain-throw/chain") }
|
||||
@ -242,9 +258,9 @@ function assertAsyncDone(iteration) {
|
||||
})();
|
||||
|
||||
(function() {
|
||||
var p1 = Promise.resolved(5)
|
||||
var p2 = Promise.resolved(p1)
|
||||
var p3 = Promise.resolved(p2)
|
||||
var p1 = Promise.resolve(5)
|
||||
var p2 = Promise.resolve(p1)
|
||||
var p3 = Promise.resolve(p2)
|
||||
p3.chain(function(x) { throw 6 }, assertUnreachable).then(
|
||||
assertUnreachable,
|
||||
function(x) { assertAsync(x === 6, "resolved/chain-throw/then") }
|
||||
@ -253,9 +269,9 @@ function assertAsyncDone(iteration) {
|
||||
})();
|
||||
|
||||
(function() {
|
||||
var p1 = Promise.resolved(5)
|
||||
var p2 = Promise.resolved(p1)
|
||||
var p3 = Promise.resolved(p2)
|
||||
var p1 = Promise.resolve(5)
|
||||
var p2 = Promise.resolve(p1)
|
||||
var p3 = Promise.resolve(p2)
|
||||
p3.then(function(x) { throw 6 }, assertUnreachable).chain(
|
||||
assertUnreachable,
|
||||
function(x) { assertAsync(x === 6, "resolved/then-throw/chain") }
|
||||
@ -264,9 +280,9 @@ function assertAsyncDone(iteration) {
|
||||
})();
|
||||
|
||||
(function() {
|
||||
var p1 = Promise.resolved(5)
|
||||
var p2 = Promise.resolved(p1)
|
||||
var p3 = Promise.resolved(p2)
|
||||
var p1 = Promise.resolve(5)
|
||||
var p2 = Promise.resolve(p1)
|
||||
var p3 = Promise.resolve(p2)
|
||||
p3.then(function(x) { throw 6 }, assertUnreachable).then(
|
||||
assertUnreachable,
|
||||
function(x) { assertAsync(x === 6, "resolved/then-throw/then") }
|
||||
@ -275,9 +291,9 @@ function assertAsyncDone(iteration) {
|
||||
})();
|
||||
|
||||
(function() {
|
||||
var p1 = Promise.resolved(5)
|
||||
var p1 = Promise.resolve(5)
|
||||
var p2 = {then: function(onResolve, onReject) { onResolve(p1) }}
|
||||
var p3 = Promise.resolved(p2)
|
||||
var p3 = Promise.resolve(p2)
|
||||
p3.chain(
|
||||
function(x) { assertAsync(x === p2, "resolved/thenable/chain") },
|
||||
assertUnreachable
|
||||
@ -286,9 +302,9 @@ function assertAsyncDone(iteration) {
|
||||
})();
|
||||
|
||||
(function() {
|
||||
var p1 = Promise.resolved(5)
|
||||
var p1 = Promise.resolve(5)
|
||||
var p2 = {then: function(onResolve, onReject) { onResolve(p1) }}
|
||||
var p3 = Promise.resolved(p2)
|
||||
var p3 = Promise.resolve(p2)
|
||||
p3.then(
|
||||
function(x) { assertAsync(x === 5, "resolved/thenable/then") },
|
||||
assertUnreachable
|
||||
@ -297,9 +313,9 @@ function assertAsyncDone(iteration) {
|
||||
})();
|
||||
|
||||
(function() {
|
||||
var p1 = Promise.rejected(5)
|
||||
var p1 = Promise.reject(5)
|
||||
var p2 = {then: function(onResolve, onReject) { onResolve(p1) }}
|
||||
var p3 = Promise.resolved(p2)
|
||||
var p3 = Promise.resolve(p2)
|
||||
p3.chain(
|
||||
function(x) { assertAsync(x === p2, "rejected/thenable/chain") },
|
||||
assertUnreachable
|
||||
@ -308,9 +324,9 @@ function assertAsyncDone(iteration) {
|
||||
})();
|
||||
|
||||
(function() {
|
||||
var p1 = Promise.rejected(5)
|
||||
var p1 = Promise.reject(5)
|
||||
var p2 = {then: function(onResolve, onReject) { onResolve(p1) }}
|
||||
var p3 = Promise.resolved(p2)
|
||||
var p3 = Promise.resolve(p2)
|
||||
p3.then(
|
||||
assertUnreachable,
|
||||
function(x) { assertAsync(x === 5, "rejected/thenable/then") }
|
||||
@ -319,10 +335,10 @@ function assertAsyncDone(iteration) {
|
||||
})();
|
||||
|
||||
(function() {
|
||||
var deferred = Promise.deferred()
|
||||
var deferred = Promise.defer()
|
||||
var p1 = deferred.promise
|
||||
var p2 = Promise.resolved(p1)
|
||||
var p3 = Promise.resolved(p2)
|
||||
var p2 = Promise.resolve(p1)
|
||||
var p3 = Promise.resolve(p2)
|
||||
p3.chain(
|
||||
function(x) { assertAsync(x === p2, "chain/resolve") },
|
||||
assertUnreachable
|
||||
@ -332,10 +348,10 @@ function assertAsyncDone(iteration) {
|
||||
})();
|
||||
|
||||
(function() {
|
||||
var deferred = Promise.deferred()
|
||||
var deferred = Promise.defer()
|
||||
var p1 = deferred.promise
|
||||
var p2 = Promise.resolved(p1)
|
||||
var p3 = Promise.resolved(p2)
|
||||
var p2 = Promise.resolve(p1)
|
||||
var p3 = Promise.resolve(p2)
|
||||
p3.then(
|
||||
function(x) { assertAsync(x === 5, "then/resolve") },
|
||||
assertUnreachable
|
||||
@ -345,10 +361,10 @@ function assertAsyncDone(iteration) {
|
||||
})();
|
||||
|
||||
(function() {
|
||||
var deferred = Promise.deferred()
|
||||
var deferred = Promise.defer()
|
||||
var p1 = deferred.promise
|
||||
var p2 = Promise.resolved(p1)
|
||||
var p3 = Promise.resolved(p2)
|
||||
var p2 = Promise.resolve(p1)
|
||||
var p3 = Promise.resolve(p2)
|
||||
p3.chain(
|
||||
function(x) { assertAsync(x === p2, "chain/reject") },
|
||||
assertUnreachable
|
||||
@ -358,10 +374,10 @@ function assertAsyncDone(iteration) {
|
||||
})();
|
||||
|
||||
(function() {
|
||||
var deferred = Promise.deferred()
|
||||
var deferred = Promise.defer()
|
||||
var p1 = deferred.promise
|
||||
var p2 = Promise.resolved(p1)
|
||||
var p3 = Promise.resolved(p2)
|
||||
var p2 = Promise.resolve(p1)
|
||||
var p3 = Promise.resolve(p2)
|
||||
p3.then(
|
||||
assertUnreachable,
|
||||
function(x) { assertAsync(x === 5, "then/reject") }
|
||||
@ -371,10 +387,10 @@ function assertAsyncDone(iteration) {
|
||||
})();
|
||||
|
||||
(function() {
|
||||
var deferred = Promise.deferred()
|
||||
var deferred = Promise.defer()
|
||||
var p1 = deferred.promise
|
||||
var p2 = {then: function(onResolve, onReject) { onResolve(p1) }}
|
||||
var p3 = Promise.resolved(p2)
|
||||
var p3 = Promise.resolve(p2)
|
||||
p3.chain(
|
||||
function(x) { assertAsync(x === p2, "chain/resolve/thenable") },
|
||||
assertUnreachable
|
||||
@ -384,10 +400,10 @@ function assertAsyncDone(iteration) {
|
||||
})();
|
||||
|
||||
(function() {
|
||||
var deferred = Promise.deferred()
|
||||
var deferred = Promise.defer()
|
||||
var p1 = deferred.promise
|
||||
var p2 = {then: function(onResolve, onReject) { onResolve(p1) }}
|
||||
var p3 = Promise.resolved(p2)
|
||||
var p3 = Promise.resolve(p2)
|
||||
p3.then(
|
||||
function(x) { assertAsync(x === 5, "then/resolve/thenable") },
|
||||
assertUnreachable
|
||||
@ -397,10 +413,10 @@ function assertAsyncDone(iteration) {
|
||||
})();
|
||||
|
||||
(function() {
|
||||
var deferred = Promise.deferred()
|
||||
var deferred = Promise.defer()
|
||||
var p1 = deferred.promise
|
||||
var p2 = {then: function(onResolve, onReject) { onResolve(p1) }}
|
||||
var p3 = Promise.resolved(p2)
|
||||
var p3 = Promise.resolve(p2)
|
||||
p3.chain(
|
||||
function(x) { assertAsync(x === p2, "chain/reject/thenable") },
|
||||
assertUnreachable
|
||||
@ -410,10 +426,10 @@ function assertAsyncDone(iteration) {
|
||||
})();
|
||||
|
||||
(function() {
|
||||
var deferred = Promise.deferred()
|
||||
var deferred = Promise.defer()
|
||||
var p1 = deferred.promise
|
||||
var p2 = {then: function(onResolve, onReject) { onResolve(p1) }}
|
||||
var p3 = Promise.resolved(p2)
|
||||
var p3 = Promise.resolve(p2)
|
||||
p3.then(
|
||||
assertUnreachable,
|
||||
function(x) { assertAsync(x === 5, "then/reject/thenable") }
|
||||
@ -423,9 +439,9 @@ function assertAsyncDone(iteration) {
|
||||
})();
|
||||
|
||||
(function() {
|
||||
var p1 = Promise.resolved(5)
|
||||
var p2 = Promise.resolved(p1)
|
||||
var deferred = Promise.deferred()
|
||||
var p1 = Promise.resolve(5)
|
||||
var p2 = Promise.resolve(p1)
|
||||
var deferred = Promise.defer()
|
||||
var p3 = deferred.promise
|
||||
p3.chain(
|
||||
function(x) { assertAsync(x === p2, "chain/resolve2") },
|
||||
@ -436,9 +452,9 @@ function assertAsyncDone(iteration) {
|
||||
})();
|
||||
|
||||
(function() {
|
||||
var p1 = Promise.resolved(5)
|
||||
var p2 = Promise.resolved(p1)
|
||||
var deferred = Promise.deferred()
|
||||
var p1 = Promise.resolve(5)
|
||||
var p2 = Promise.resolve(p1)
|
||||
var deferred = Promise.defer()
|
||||
var p3 = deferred.promise
|
||||
p3.then(
|
||||
function(x) { assertAsync(x === 5, "then/resolve2") },
|
||||
@ -449,9 +465,9 @@ function assertAsyncDone(iteration) {
|
||||
})();
|
||||
|
||||
(function() {
|
||||
var p1 = Promise.resolved(5)
|
||||
var p2 = Promise.resolved(p1)
|
||||
var deferred = Promise.deferred()
|
||||
var p1 = Promise.resolve(5)
|
||||
var p2 = Promise.resolve(p1)
|
||||
var deferred = Promise.defer()
|
||||
var p3 = deferred.promise
|
||||
p3.chain(
|
||||
assertUnreachable,
|
||||
@ -462,9 +478,9 @@ function assertAsyncDone(iteration) {
|
||||
})();
|
||||
|
||||
(function() {
|
||||
var p1 = Promise.resolved(5)
|
||||
var p2 = Promise.resolved(p1)
|
||||
var deferred = Promise.deferred()
|
||||
var p1 = Promise.resolve(5)
|
||||
var p2 = Promise.resolve(p1)
|
||||
var deferred = Promise.defer()
|
||||
var p3 = deferred.promise
|
||||
p3.then(
|
||||
assertUnreachable,
|
||||
@ -475,9 +491,9 @@ function assertAsyncDone(iteration) {
|
||||
})();
|
||||
|
||||
(function() {
|
||||
var p1 = Promise.resolved(5)
|
||||
var p1 = Promise.resolve(5)
|
||||
var p2 = {then: function(onResolve, onReject) { onResolve(p1) }}
|
||||
var deferred = Promise.deferred()
|
||||
var deferred = Promise.defer()
|
||||
var p3 = deferred.promise
|
||||
p3.chain(
|
||||
function(x) { assertAsync(x === p2, "chain/resolve/thenable2") },
|
||||
@ -488,9 +504,9 @@ function assertAsyncDone(iteration) {
|
||||
})();
|
||||
|
||||
(function() {
|
||||
var p1 = Promise.resolved(5)
|
||||
var p1 = Promise.resolve(5)
|
||||
var p2 = {then: function(onResolve, onReject) { onResolve(p1) }}
|
||||
var deferred = Promise.deferred()
|
||||
var deferred = Promise.defer()
|
||||
var p3 = deferred.promise
|
||||
p3.then(
|
||||
function(x) { assertAsync(x === 5, "then/resolve/thenable2") },
|
||||
@ -501,7 +517,7 @@ function assertAsyncDone(iteration) {
|
||||
})();
|
||||
|
||||
(function() {
|
||||
var p1 = Promise.resolved(0)
|
||||
var p1 = Promise.resolve(0)
|
||||
var p2 = p1.chain(function(x) { return p2 }, assertUnreachable)
|
||||
p2.chain(
|
||||
assertUnreachable,
|
||||
@ -511,7 +527,7 @@ function assertAsyncDone(iteration) {
|
||||
})();
|
||||
|
||||
(function() {
|
||||
var p1 = Promise.resolved(0)
|
||||
var p1 = Promise.resolve(0)
|
||||
var p2 = p1.then(function(x) { return p2 }, assertUnreachable)
|
||||
p2.chain(
|
||||
assertUnreachable,
|
||||
@ -521,7 +537,7 @@ function assertAsyncDone(iteration) {
|
||||
})();
|
||||
|
||||
(function() {
|
||||
var deferred = Promise.deferred()
|
||||
var deferred = Promise.defer()
|
||||
var p = deferred.promise
|
||||
deferred.resolve(p)
|
||||
p.chain(
|
||||
@ -532,7 +548,7 @@ function assertAsyncDone(iteration) {
|
||||
})();
|
||||
|
||||
(function() {
|
||||
var deferred = Promise.deferred()
|
||||
var deferred = Promise.defer()
|
||||
var p = deferred.promise
|
||||
deferred.resolve(p)
|
||||
p.then(
|
||||
@ -542,6 +558,14 @@ function assertAsyncDone(iteration) {
|
||||
assertAsyncRan()
|
||||
})();
|
||||
|
||||
(function() {
|
||||
Promise.all({get length() { throw 666 }}).chain(
|
||||
assertUnreachable,
|
||||
function(r) { assertAsync(r === 666, "all/no-array") }
|
||||
)
|
||||
assertAsyncRan()
|
||||
})();
|
||||
|
||||
(function() {
|
||||
Promise.all([]).chain(
|
||||
function(x) { assertAsync(x.length === 0, "all/resolve/empty") },
|
||||
@ -551,11 +575,11 @@ function assertAsyncDone(iteration) {
|
||||
})();
|
||||
|
||||
(function() {
|
||||
var deferred1 = Promise.deferred()
|
||||
var deferred1 = Promise.defer()
|
||||
var p1 = deferred1.promise
|
||||
var deferred2 = Promise.deferred()
|
||||
var deferred2 = Promise.defer()
|
||||
var p2 = deferred2.promise
|
||||
var deferred3 = Promise.deferred()
|
||||
var deferred3 = Promise.defer()
|
||||
var p3 = deferred3.promise
|
||||
Promise.all([p1, p2, p3]).chain(
|
||||
function(x) {
|
||||
@ -576,10 +600,10 @@ function assertAsyncDone(iteration) {
|
||||
})();
|
||||
|
||||
(function() {
|
||||
var deferred = Promise.deferred()
|
||||
var deferred = Promise.defer()
|
||||
var p1 = deferred.promise
|
||||
var p2 = Promise.resolved(2)
|
||||
var p3 = Promise.deferred().promise
|
||||
var p2 = Promise.resolve(2)
|
||||
var p3 = Promise.defer().promise
|
||||
Promise.all([p1, p2, p3]).chain(
|
||||
assertUnreachable,
|
||||
assertUnreachable
|
||||
@ -588,11 +612,11 @@ function assertAsyncDone(iteration) {
|
||||
})();
|
||||
|
||||
(function() {
|
||||
var deferred1 = Promise.deferred()
|
||||
var deferred1 = Promise.defer()
|
||||
var p1 = deferred1.promise
|
||||
var deferred2 = Promise.deferred()
|
||||
var deferred2 = Promise.defer()
|
||||
var p2 = deferred2.promise
|
||||
var deferred3 = Promise.deferred()
|
||||
var deferred3 = Promise.defer()
|
||||
var p3 = deferred3.promise
|
||||
Promise.all([p1, p2, p3]).chain(
|
||||
assertUnreachable,
|
||||
@ -605,42 +629,50 @@ function assertAsyncDone(iteration) {
|
||||
})();
|
||||
|
||||
(function() {
|
||||
Promise.one([]).chain(
|
||||
Promise.race([]).chain(
|
||||
assertUnreachable,
|
||||
assertUnreachable
|
||||
)
|
||||
})();
|
||||
|
||||
(function() {
|
||||
var p1 = Promise.resolved(1)
|
||||
var p2 = Promise.resolved(2)
|
||||
var p3 = Promise.resolved(3)
|
||||
Promise.one([p1, p2, p3]).chain(
|
||||
function(x) { assertAsync(x === 1, "resolved/all") },
|
||||
var p1 = Promise.resolve(1)
|
||||
var p2 = Promise.resolve(2)
|
||||
var p3 = Promise.resolve(3)
|
||||
Promise.race([p1, p2, p3]).chain(
|
||||
function(x) { assertAsync(x === 1, "resolved/one") },
|
||||
assertUnreachable
|
||||
)
|
||||
assertAsyncRan()
|
||||
})();
|
||||
|
||||
(function() {
|
||||
var p1 = Promise.resolved(1)
|
||||
var p2 = Promise.resolved(2)
|
||||
var p3 = Promise.resolved(3)
|
||||
Promise.one([0, p1, p2, p3]).chain(
|
||||
function(x) { assertAsync(x === 0, "resolved-const/all") },
|
||||
var p1 = Promise.resolve(1)
|
||||
var p2 = Promise.resolve(2)
|
||||
var p3 = Promise.resolve(3)
|
||||
Promise.race([0, p1, p2, p3]).chain(
|
||||
function(x) { assertAsync(x === 0, "resolved-const/one") },
|
||||
assertUnreachable
|
||||
)
|
||||
assertAsyncRan()
|
||||
})();
|
||||
|
||||
(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 deferred2 = Promise.deferred()
|
||||
var deferred2 = Promise.defer()
|
||||
var p2 = deferred2.promise
|
||||
var deferred3 = Promise.deferred()
|
||||
var deferred3 = Promise.defer()
|
||||
var p3 = deferred3.promise
|
||||
Promise.one([p1, p2, p3]).chain(
|
||||
Promise.race([p1, p2, p3]).chain(
|
||||
function(x) { assertAsync(x === 3, "one/resolve") },
|
||||
assertUnreachable
|
||||
)
|
||||
@ -650,11 +682,11 @@ function assertAsyncDone(iteration) {
|
||||
})();
|
||||
|
||||
(function() {
|
||||
var deferred = Promise.deferred()
|
||||
var deferred = Promise.defer()
|
||||
var p1 = deferred.promise
|
||||
var p2 = Promise.resolved(2)
|
||||
var p3 = Promise.deferred().promise
|
||||
Promise.one([p1, p2, p3]).chain(
|
||||
var p2 = Promise.resolve(2)
|
||||
var p3 = Promise.defer().promise
|
||||
Promise.race([p1, p2, p3]).chain(
|
||||
function(x) { assertAsync(x === 2, "resolved/one") },
|
||||
assertUnreachable
|
||||
)
|
||||
@ -663,13 +695,13 @@ function assertAsyncDone(iteration) {
|
||||
})();
|
||||
|
||||
(function() {
|
||||
var deferred1 = Promise.deferred()
|
||||
var deferred1 = Promise.defer()
|
||||
var p1 = deferred1.promise
|
||||
var deferred2 = Promise.deferred()
|
||||
var deferred2 = Promise.defer()
|
||||
var p2 = deferred2.promise
|
||||
var deferred3 = Promise.deferred()
|
||||
var deferred3 = Promise.defer()
|
||||
var p3 = deferred3.promise
|
||||
Promise.one([p1, p2, p3]).chain(
|
||||
Promise.race([p1, p2, p3]).chain(
|
||||
function(x) { assertAsync(x === 3, "one/resolve/reject") },
|
||||
assertUnreachable
|
||||
)
|
||||
@ -679,13 +711,13 @@ function assertAsyncDone(iteration) {
|
||||
})();
|
||||
|
||||
(function() {
|
||||
var deferred1 = Promise.deferred()
|
||||
var deferred1 = Promise.defer()
|
||||
var p1 = deferred1.promise
|
||||
var deferred2 = Promise.deferred()
|
||||
var deferred2 = Promise.defer()
|
||||
var p2 = deferred2.promise
|
||||
var deferred3 = Promise.deferred()
|
||||
var deferred3 = Promise.defer()
|
||||
var p3 = deferred3.promise
|
||||
Promise.one([p1, p2, p3]).chain(
|
||||
Promise.race([p1, p2, p3]).chain(
|
||||
assertUnreachable,
|
||||
function(x) { assertAsync(x === 3, "one/reject/resolve") }
|
||||
)
|
||||
@ -698,56 +730,61 @@ function assertAsyncDone(iteration) {
|
||||
var log
|
||||
function MyPromise(resolver) {
|
||||
log += "n"
|
||||
Promise.call(this,
|
||||
function(resolve, reject) {
|
||||
resolver(
|
||||
function(x) { log += "x" + x; resolve(x) },
|
||||
function(r) { log += "r" + r; reject(r) }
|
||||
)
|
||||
}
|
||||
)
|
||||
var promise = new Promise(function(resolve, reject) {
|
||||
resolver(
|
||||
function(x) { log += "x" + x; resolve(x) },
|
||||
function(r) { log += "r" + r; reject(r) }
|
||||
)
|
||||
})
|
||||
promise.__proto__ = MyPromise.prototype
|
||||
return promise
|
||||
}
|
||||
|
||||
MyPromise.__proto__ = Promise
|
||||
MyPromise.deferred = function() {
|
||||
MyPromise.defer = function() {
|
||||
log += "d"
|
||||
return this.__proto__.deferred.call(this)
|
||||
return this.__proto__.defer.call(this)
|
||||
}
|
||||
|
||||
MyPromise.prototype.__proto__ = Promise.prototype
|
||||
MyPromise.prototype.chain = function(resolve, reject) {
|
||||
log += "w"
|
||||
log += "c"
|
||||
return this.__proto__.__proto__.chain.call(this, resolve, reject)
|
||||
}
|
||||
|
||||
log = ""
|
||||
var p1 = new MyPromise(function(resolve, reject) { resolve(1) })
|
||||
var p2 = new MyPromise(function(resolve, reject) { reject(2) })
|
||||
var d3 = MyPromise.deferred()
|
||||
assertTrue(d3.promise instanceof MyPromise, "subclass/instance3")
|
||||
var d3 = MyPromise.defer()
|
||||
assertTrue(d3.promise instanceof Promise, "subclass/instance")
|
||||
assertTrue(d3.promise instanceof MyPromise, "subclass/instance-my3")
|
||||
assertTrue(log === "nx1nr2dn", "subclass/create")
|
||||
|
||||
log = ""
|
||||
var p4 = MyPromise.resolved(4)
|
||||
var p5 = MyPromise.rejected(5)
|
||||
assertTrue(p4 instanceof MyPromise, "subclass/instance4")
|
||||
assertTrue(p5 instanceof MyPromise, "subclass/instance5")
|
||||
var p4 = MyPromise.resolve(4)
|
||||
var p5 = MyPromise.reject(5)
|
||||
assertTrue(p4 instanceof Promise, "subclass/instance4")
|
||||
assertTrue(p4 instanceof MyPromise, "subclass/instance-my4")
|
||||
assertTrue(p5 instanceof Promise, "subclass/instance5")
|
||||
assertTrue(p5 instanceof MyPromise, "subclass/instance-my5")
|
||||
d3.resolve(3)
|
||||
assertTrue(log === "nx4nr5x3", "subclass/resolve")
|
||||
|
||||
log = ""
|
||||
var d6 = MyPromise.deferred()
|
||||
d6.promise.chain(function(x) { return new Promise(x) }).chain(function() {})
|
||||
var d6 = MyPromise.defer()
|
||||
d6.promise.chain(function(x) {
|
||||
return new Promise(function(resolve) { resolve(x) })
|
||||
}).chain(function() {})
|
||||
d6.resolve(6)
|
||||
assertTrue(log === "dnwnwnx6", "subclass/chain")
|
||||
assertTrue(log === "dncncnx6", "subclass/chain")
|
||||
|
||||
log = ""
|
||||
Promise.all([11, Promise.resolved(12), 13, MyPromise.resolved(14), 15, 16])
|
||||
assertTrue(log === "nx14wn", "subclass/all/arg")
|
||||
Promise.all([11, Promise.resolve(12), 13, MyPromise.resolve(14), 15, 16])
|
||||
assertTrue(log === "nx14cn", "subclass/all/arg")
|
||||
|
||||
log = ""
|
||||
MyPromise.all([21, Promise.resolved(22), 23, MyPromise.resolved(24), 25, 26])
|
||||
assertTrue(log === "nx24dnnx21wnnx23wnwnnx25wnnx26wn", "subclass/all/self")
|
||||
MyPromise.all([21, Promise.resolve(22), 23, MyPromise.resolve(24), 25, 26])
|
||||
assertTrue(log === "nx24nnx21cnnx23cncnnx25cnnx26cn", "subclass/all/self")
|
||||
})();
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user