Skip to content
This repository has been archived by the owner on Oct 7, 2019. It is now read-only.

Commit

Permalink
Merge pull request #11 from showpad/spread-on-promise-alternative
Browse files Browse the repository at this point in the history
Spread on promise alternative
  • Loading branch information
klaascuvelier committed Aug 18, 2015
2 parents 9b7d922 + b82a032 commit 69a4a23
Show file tree
Hide file tree
Showing 9 changed files with 287 additions and 130 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# angular-q-spread changelog

### 1.1.0
* Add `spread` on every promise

### 1.0.4
* Use minified version in bower config

Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@
angular-q-spread
================

Add `spread` method to the promise returned by $q.all.
Add `spread` method to all promises created by the $q service.

`spread` can be used as a replacement for `then`. Similarly, it takes two parameters, a callback when all promises are resolved and a callback for failure.
The resolve callback is going to be called with the result of the list of promises passed to $q.all as separate parameters instead of one parameters which is an array.

`spread` can be chained onto every promise.

#Compatibility
This plugin has been tested with Angular 1.2 and 1.3

Expand Down
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "angular-q-spread",
"version": "1.0.4",
"version": "1.1.0",
"homepage": "https://github.com/showpad/angular-q-spread",
"description": "Add `spread` method to the promise of $q.all",
"main": "dist/q-spread.min.js",
Expand Down
42 changes: 21 additions & 21 deletions dist/q-spread.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,29 @@

$provide.decorator('$q', ['$delegate', 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) {
return resolve.apply(void 0, data);
}

return promise.then(spread, reject);
};

return promise;
var originalDefer = $delegate.defer;

$delegate.defer = function () {
// Get the prototype of the promise
var promiseProto = originalDefer().promise.constructor.prototype;

// Add the spread method
Object.defineProperty(promiseProto, 'spread', {
value: function (resolve, reject) {
function spread (data) {
return resolve.apply(void 0, data);
}

return this.then(spread, reject);
},
writable: true,
enumerable: false
});
return originalDefer();
};

return $delegate;
}]);
}]);
})(window.angular);
})(window.angular);
2 changes: 1 addition & 1 deletion dist/q-spread.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ gulp.task('karma', function (done) {
configFile: config.test,
singleRun: true
},
done
function () {
done();
}

);

server.start();
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "angular-q-spread",
"version": "1.0.4",
"version": "1.1.0",
"description": "Add `spread` method to the promise of $q.all",
"main": "index.js",
"scripts": {
Expand Down
42 changes: 21 additions & 21 deletions src/q-spread.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,29 @@

$provide.decorator('$q', ['$delegate', 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) {
return resolve.apply(void 0, data);
}

return promise.then(spread, reject);
};

return promise;
var originalDefer = $delegate.defer;

$delegate.defer = function () {
// Get the prototype of the promise
var promiseProto = originalDefer().promise.constructor.prototype;

// Add the spread method
Object.defineProperty(promiseProto, 'spread', {
value: function (resolve, reject) {
function spread (data) {
return resolve.apply(void 0, data);
}

return this.then(spread, reject);
},
writable: true,
enumerable: false
});
return originalDefer();
};

return $delegate;
}]);
}]);
})(window.angular);
})(window.angular);
Loading

0 comments on commit 69a4a23

Please sign in to comment.