Commit 2ac95e1d authored by Tristan Cavelier's avatar Tristan Cavelier

Promise.js success static method added

parent 083f8f7a
...@@ -75,6 +75,32 @@ Promise.error = function (item, onError) { ...@@ -75,6 +75,32 @@ Promise.error = function (item, onError) {
return p; return p;
}; };
/**
* success(item, [onSuccess]): Promise
*
* Return an item as first parameter of the promise answer. The method returns a
* resolved promise.
*
* Promise.success(errorPromise).then(console.log); // shows 'Error'
* Promise.success(Promise.error('a')).then(console.log); // shows 'a'
*
* @method success
* @static
* @param {Any} item The item to use
* @param {Function} [onSuccess] the callback called on success
* @return {Promise} The promise
*/
Promise.success = function (item, onSuccess) {
var p = new Promise().done(onSuccess), solver = p.defer();
Promise.when(
item,
solver.resolve.bind(solver),
solver.resolve.bind(solver),
solver.notify.bind(solver)
);
return p;
};
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
// http://wiki.commonjs.org/wiki/Promises/B // http://wiki.commonjs.org/wiki/Promises/B
// get(object, name) // get(object, name)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment