Skip to content
This repository has been archived by the owner on Apr 12, 2024. It is now read-only.

Commit

Permalink
fix(q): resolve all of nothing to nothing
Browse files Browse the repository at this point in the history
$q.all([]) no longer throws exception and resolves to empty array []
  • Loading branch information
mhevery committed Mar 23, 2012
1 parent 5390fb3 commit ac75079
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
22 changes: 13 additions & 9 deletions src/service/q.js
Original file line number Diff line number Diff line change
Expand Up @@ -364,16 +364,20 @@ function qFactory(nextTick, exceptionHandler) {
counter = promises.length,
results = [];

forEach(promises, function(promise, index) {
promise.then(function(value) {
if (index in results) return;
results[index] = value;
if (!(--counter)) deferred.resolve(results);
}, function(reason) {
if (index in results) return;
deferred.reject(reason);
if (counter) {
forEach(promises, function(promise, index) {
ref(promise).then(function(value) {
if (index in results) return;
results[index] = value;
if (!(--counter)) deferred.resolve(results);
}, function(reason) {
if (index in results) return;
deferred.reject(reason);
});
});
});
} else {
deferred.resolve(results);
}

return deferred.promise;
}
Expand Down
8 changes: 8 additions & 0 deletions test/service/qSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -676,6 +676,14 @@ describe('q', function() {


describe('all', function() {
it('should resolve all of nothing', function() {
var result;
q.all([]).then(function(r) { result = r; });
mockNextTick.flush();
expect(result).toEqual([]);
});


it('should take an array of promises and return a promise for an array of results', function() {
var deferred1 = defer(),
deferred2 = defer();
Expand Down

0 comments on commit ac75079

Please sign in to comment.