Skip to content

Commit

Permalink
Speed up Q.all
Browse files Browse the repository at this point in the history
In the case where either:

 a) Not all values in the input array are promises (many orders of magnitude improvement)
 b) Some of the promises are already resolve (about twice as quick).

It does this by not waiting till the next turn of the event loop when it is un-necessary to do so.

Deferred will already ensure that the overall behaviour of all is correct with regard to resolving exactly once.
  • Loading branch information
ForbesLindesay committed Jul 29, 2012
1 parent 5701e19 commit 0f16f4a
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions q.js
Original file line number Diff line number Diff line change
Expand Up @@ -1227,13 +1227,20 @@ function all(promises) {
}
var deferred = defer();
array_reduce(promises, function (undefined, promise, index) {
when(promise, function (value) {
promises[index] = value;
if (isFulfilled(promise)) {
promises[index] = valueOf(promise);
if (--countDown === 0) {
deferred.resolve(promises);
}
})
.fail(deferred.reject);
} else {
when(promise, function (value) {
promises[index] = value;
if (--countDown === 0) {
deferred.resolve(promises);
}
})
.fail(deferred.reject);
}
}, void 0);
return deferred.promise;
});
Expand Down

0 comments on commit 0f16f4a

Please sign in to comment.