diff --git a/src/service/q.js b/src/service/q.js index f0c030b06750..074acd1de447 100644 --- a/src/service/q.js +++ b/src/service/q.js @@ -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; } diff --git a/test/service/qSpec.js b/test/service/qSpec.js index e592ab87a9e0..a230d1de5ee7 100644 --- a/test/service/qSpec.js +++ b/test/service/qSpec.js @@ -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();