Skip to content

Commit

Permalink
Rename Promise.cast to Promise.resolve; remove old Promise.resolve.
Browse files Browse the repository at this point in the history
  • Loading branch information
cscott committed Feb 14, 2014
1 parent a06e548 commit 99c8d79
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 16 deletions.
20 changes: 6 additions & 14 deletions es6-shim.js
Original file line number Diff line number Diff line change
Expand Up @@ -1190,7 +1190,7 @@
if (next.done) {
break;
}
var nextPromise = C.cast(next.value);
var nextPromise = C.resolve(next.value);
var resolveElement = _promiseAllResolver(
index, values, capability, remaining
);
Expand All @@ -1206,18 +1206,6 @@
return capability.promise;
};

Promise.cast = function(x) {
var C = this;
if (ES.IsPromise(x)) {
var constructor = x._promiseConstructor;
if (constructor === C) { return x; }
}
var capability = new PromiseCapability(C);
var resolve = capability.resolve;
resolve(x); // call with this===undefined
return capability.promise;
};

Promise.race = function(iterable) {
var C = this;
var capability = new PromiseCapability(C);
Expand All @@ -1237,7 +1225,7 @@
// https://bugs.ecmascript.org/show_bug.cgi?id=2515
break;
}
var nextPromise = C.cast(next.value);
var nextPromise = C.resolve(next.value);
nextPromise.then(resolve, reject);
}
} catch (e) {
Expand All @@ -1256,6 +1244,10 @@

Promise.resolve = function(v) {
var C = this;
if (ES.IsPromise(v)) {
var constructor = v._promiseConstructor;
if (constructor === C) { return v; }
}
var capability = new PromiseCapability(C);
var resolve = capability.resolve;
resolve(v); // call with this===undefined
Expand Down
2 changes: 1 addition & 1 deletion test/promise/all.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ describe("Promise.all", function () {
P.__proto__ = Promise;
P.prototype = Object.create(Promise.prototype);
P.prototype.constructor = P;
P.cast = function(p) { return p; };
P.resolve = function(p) { return p; };

var g = [
Promise.resolve(0),
Expand Down
10 changes: 9 additions & 1 deletion test/promise/evil-promises.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,15 @@ var assert = require("assert");

describe("Evil promises should not be able to break invariants", function () {
specify("resolving to a promise that calls onFulfilled twice", function (done) {
var evilPromise = Promise.resolve();
// note that we have to create a trivial subclass, as otherwise the
// Promise.resolve(evilPromise) is just the identity function.
var EvilPromise = function(executor) { Promise.call(this, executor); };
if (!EvilPromise.__proto__) { return; } // skip test if on IE < 11
EvilPromise.__proto__ = Promise; // mutable __proto__ is in es6.
EvilPromise.prototype = Object.create(Promise.prototype);
EvilPromise.prototype.constructor = EvilPromise;

var evilPromise = EvilPromise.resolve();
evilPromise.then = function (f) {
f(1);
f(2);
Expand Down

0 comments on commit 99c8d79

Please sign in to comment.