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

Commit ac75079

Browse files
committed
fix(q): resolve all of nothing to nothing
$q.all([]) no longer throws exception and resolves to empty array []
1 parent 5390fb3 commit ac75079

File tree

2 files changed

+21
-9
lines changed

2 files changed

+21
-9
lines changed

Diff for: src/service/q.js

+13-9
Original file line numberDiff line numberDiff line change
@@ -364,16 +364,20 @@ function qFactory(nextTick, exceptionHandler) {
364364
counter = promises.length,
365365
results = [];
366366

367-
forEach(promises, function(promise, index) {
368-
promise.then(function(value) {
369-
if (index in results) return;
370-
results[index] = value;
371-
if (!(--counter)) deferred.resolve(results);
372-
}, function(reason) {
373-
if (index in results) return;
374-
deferred.reject(reason);
367+
if (counter) {
368+
forEach(promises, function(promise, index) {
369+
ref(promise).then(function(value) {
370+
if (index in results) return;
371+
results[index] = value;
372+
if (!(--counter)) deferred.resolve(results);
373+
}, function(reason) {
374+
if (index in results) return;
375+
deferred.reject(reason);
376+
});
375377
});
376-
});
378+
} else {
379+
deferred.resolve(results);
380+
}
377381

378382
return deferred.promise;
379383
}

Diff for: test/service/qSpec.js

+8
Original file line numberDiff line numberDiff line change
@@ -676,6 +676,14 @@ describe('q', function() {
676676

677677

678678
describe('all', function() {
679+
it('should resolve all of nothing', function() {
680+
var result;
681+
q.all([]).then(function(r) { result = r; });
682+
mockNextTick.flush();
683+
expect(result).toEqual([]);
684+
});
685+
686+
679687
it('should take an array of promises and return a promise for an array of results', function() {
680688
var deferred1 = defer(),
681689
deferred2 = defer();

0 commit comments

Comments
 (0)