Skip to content

Commit

Permalink
Merge pull request kriskowal#740 from jbunton/any_gives_rejection_error
Browse files Browse the repository at this point in the history
Q.any gives an error message from last rejected promise
  • Loading branch information
kriskowal committed Oct 30, 2015
2 parents 26ae6a2 + c340b2d commit 6f174ca
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
- Q.any gives an error message from the last rejected promise
- Throw if callback supplied to "finally" is invalid (@grahamrhay)

## 1.4.1
Expand Down
9 changes: 4 additions & 5 deletions q.js
Original file line number Diff line number Diff line change
Expand Up @@ -1622,13 +1622,12 @@ function any(promises) {
function onFulfilled(result) {
deferred.resolve(result);
}
function onRejected() {
function onRejected(err) {
pendingCount--;
if (pendingCount === 0) {
deferred.reject(new Error(
"Q can't get fulfillment value from any promise, all " +
"promises were rejected."
));
err.message = ("Q can't get fulfillment value from any promise, all " +
"promises were rejected. Last error message: " + err.message);
deferred.reject(err);
}
}
function onProgress(progress) {
Expand Down
6 changes: 4 additions & 2 deletions spec/q-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1206,19 +1206,21 @@ describe("any", function() {

function testReject(promises, deferreds) {
var promise = Q.any(promises);
var expectedError = new Error('Rejected');

for (var index = 0; index < deferreds.length; index++) {
var deferred = deferreds[index];
(function() {
deferred.reject(new Error('Rejected'));
deferred.reject(expectedError);
})();
}

return Q.delay(250)
.then(function() {
expect(promise.isRejected()).toBe(true);
expect(promise.inspect().reason).toBe(expectedError);
expect(promise.inspect().reason.message)
.toBe("Q can't get fulfillment value from any promise, all promises were rejected.");
.toBe("Q can't get fulfillment value from any promise, all promises were rejected. Last error message: Rejected");
})
.timeout(1000);
}
Expand Down

0 comments on commit 6f174ca

Please sign in to comment.