diff --git a/lib/promise/cast.js b/lib/promise/cast.js deleted file mode 100644 index 993ee3a..0000000 --- a/lib/promise/cast.js +++ /dev/null @@ -1,66 +0,0 @@ -/** - `RSVP.Promise.cast` returns the same promise if that promise shares a constructor - with the promise being casted. - - Example: - - ```javascript - var promise = RSVP.resolve(1); - var casted = RSVP.Promise.cast(promise); - - console.log(promise === casted); // true - ``` - - In the case of a promise whose constructor does not match, it is assimilated. - The resulting promise will fulfill or reject based on the outcome of the - promise being casted. - - In the case of a non-promise, a promise which will fulfill with that value is - returned. - - Example: - - ```javascript - var value = 1; // could be a number, boolean, string, undefined... - var casted = RSVP.Promise.cast(value); - - console.log(value === casted); // false - console.log(casted instanceof RSVP.Promise) // true - - casted.then(function(val) { - val === value // => true - }); - ``` - - `RSVP.Promise.cast` is similar to `RSVP.resolve`, but `RSVP.Promise.cast` differs in the - following ways: - * `RSVP.Promise.cast` serves as a memory-efficient way of getting a promise, when you - have something that could either be a promise or a value. RSVP.resolve - will have the same effect but will create a new promise wrapper if the - argument is a promise. - * `RSVP.Promise.cast` is a way of casting incoming thenables or promise subclasses to - promises of the exact class specified, so that the resulting object's `then` is - ensured to have the behavior of the constructor you are calling cast on (i.e., RSVP.Promise). - - @method cast - @for RSVP - @param {Object} object to be casted - @return {Promise} promise that is fulfilled when all properties of `promises` - have been fulfilled, or rejected if any of them become rejected. -*/ - - -function cast(object) { - /*jshint validthis:true */ - if (object && typeof object === 'object' && object.constructor === this) { - return object; - } - - var Promise = this; - - return new Promise(function(resolve) { - resolve(object); - }); -} - -export { cast }; diff --git a/lib/promise/polyfill.js b/lib/promise/polyfill.js index b619126..7bc7146 100644 --- a/lib/promise/polyfill.js +++ b/lib/promise/polyfill.js @@ -17,7 +17,6 @@ function polyfill() { "Promise" in local && // Some of these methods are missing from // Firefox/Chrome experimental implementations - "cast" in local.Promise && "resolve" in local.Promise && "reject" in local.Promise && "all" in local.Promise && diff --git a/lib/promise/promise.js b/lib/promise/promise.js index 32bea1e..1b544d9 100644 --- a/lib/promise/promise.js +++ b/lib/promise/promise.js @@ -1,6 +1,5 @@ import { config, configure } from "./config"; import { objectOrFunction, isFunction, now } from './utils'; -import { cast } from "./cast"; import { all } from "./all"; import { race } from "./race"; import { resolve as staticResolve } from "./resolve"; @@ -128,7 +127,6 @@ Promise.prototype = { }; Promise.all = all; -Promise.cast = cast; Promise.race = race; Promise.resolve = staticResolve; Promise.reject = staticReject; diff --git a/lib/promise/resolve.js b/lib/promise/resolve.js index de86e93..a82923d 100644 --- a/lib/promise/resolve.js +++ b/lib/promise/resolve.js @@ -1,39 +1,12 @@ -/** - `RSVP.resolve` returns a promise that will become fulfilled with the passed - `value`. `RSVP.resolve` is essentially shorthand for the following: - - ```javascript - var promise = new RSVP.Promise(function(resolve, reject){ - resolve(1); - }); - - promise.then(function(value){ - // value === 1 - }); - ``` - - Instead of writing the above, your code now simply becomes the following: - - ```javascript - var promise = RSVP.resolve(1); - - promise.then(function(value){ - // value === 1 - }); - ``` - - @method resolve - @for RSVP - @param {Any} value value that the returned promise will be resolved with - @param {String} label optional string for identifying the returned promise. - Useful for tooling. - @return {Promise} a promise that will become fulfilled with the given - `value` -*/ function resolve(value) { /*jshint validthis:true */ + if (value && typeof value === 'object' && value.constructor === this) { + return value; + } + var Promise = this; - return new Promise(function(resolve, reject) { + + return new Promise(function(resolve) { resolve(value); }); } diff --git a/test/tests/extension_test.js b/test/tests/extension_test.js index 5ac3650..17910e5 100644 --- a/test/tests/extension_test.js +++ b/test/tests/extension_test.js @@ -578,7 +578,7 @@ describe("RSVP extensions", function() { }); }); - describe("RSVP.resolve", function(){ + describe("Promise.resolve", function(){ specify("it should exist", function(){ assert(Promise.resolve); }); @@ -864,19 +864,17 @@ describe("RSVP extensions", function() { assert(callCount === 0, 'expected async, was sync'); }); }); - }); - describe("Promise.cast", function () { it("If SameValue(constructor, C) is true, return x.", function(){ var promise = Promise.resolve(1); - var casted = Promise.cast(promise); + var casted = Promise.resolve(promise); assert.deepEqual(casted, promise); }); it("If SameValue(constructor, C) is false, and isThenable(C) is true, return PromiseResolve(promise, x).", function(){ var promise = { then: function() { } }; - var casted = Promise.cast(promise); + var casted = Promise.resolve(promise); assert(casted instanceof Promise); assert(casted !== promise); @@ -889,10 +887,10 @@ describe("RSVP extensions", function() { PromiseSubclass.prototype = Object.create(Promise.prototype); PromiseSubclass.prototype.constructor = PromiseSubclass; - PromiseSubclass.cast = Promise.cast; + PromiseSubclass.resolve = Promise.resolve; var promise = Promise.resolve(1); - var casted = PromiseSubclass.cast(promise); + var casted = PromiseSubclass.resolve(promise); assert(casted instanceof Promise); assert(casted instanceof PromiseSubclass); @@ -906,14 +904,14 @@ describe("RSVP extensions", function() { it("If SameValue(constructor, C) is false, and isThenable(C) is false, return PromiseResolve(promise, x).", function(){ var value = 1; - var casted = Promise.cast(value); + var casted = Promise.resolve(value); assert(casted instanceof Promise); assert(casted !== value); }); it("casts null correctly", function(done){ - Promise.cast(null).then(function(value){ + Promise.resolve(null).then(function(value){ assert.equal(value, null); done(); });