Skip to content

Instantly share code, notes, and snippets.

@klaascuvelier
Last active August 29, 2015 14:08
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save klaascuvelier/f3e69af79200851e8ee9 to your computer and use it in GitHub Desktop.
Save klaascuvelier/f3e69af79200851e8ee9 to your computer and use it in GitHub Desktop.
Add spread method to $q.all promise
angular
.module('$q-spread', [])
.config(function ($provide) {
$provide.decorator('$q', function ($delegate) {
var originalAll = $delegate.all;
$delegate.all = function (promises) {
var promise = originalAll(promises);
/**
* Spread method, proxies to `then` but spreads the results for the resolve callback
* @param {Function} resolve
* @param {Function} reject
*/
promise.spread = function (resolve, reject) {
function spread(data) {
resolve.apply(void 0, data);
}
promise.then(spread, reject);
};
return promise;
};
return $delegate;
});
});

#Usage: var promises = [promise1, promise2];

function allSuccess(result1, result2) 
{
    // do stuff with results
}

function error() 
{
    // something went wrong
}

$q
    .all(promises)
    .spread(allSuccess, error);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment