Commit 558ac4dd authored by Romain Courteaud's avatar Romain Courteaud

Cancel remaining promises if all/any is rejected.

parent 5b7ebbb0
...@@ -48,11 +48,16 @@ function promiseAtLeast(expected_count, promises) { ...@@ -48,11 +48,16 @@ function promiseAtLeast(expected_count, promises) {
} }
} }
function cancelAll(rejectionValue) {
reject(rejectionValue);
canceller();
}
for (var i = 0; i < promises.length; i++) { for (var i = 0; i < promises.length; i++) {
promise = promises[i]; promise = promises[i];
if (promise && typeof promise.then === 'function') { if (promise && typeof promise.then === 'function') {
promise.then(resolver(i), reject); promise.then(resolver(i), cancelAll);
} else { } else {
resolveAll(i, promise); resolveAll(i, promise);
} }
......
...@@ -723,6 +723,36 @@ describe("RSVP extensions", function() { ...@@ -723,6 +723,36 @@ describe("RSVP extensions", function() {
done(); done();
}, 20); }, 20);
}); });
specify('cancel the array of promise as soon as rejected', function(done) {
var firstResolver, secondResolver;
var first = new RSVP.Promise(function(resolve, reject) {
firstResolver = { resolve: resolve, reject: reject };
});
var second = new RSVP.Promise(function(resolve, reject) {
secondResolver = { resolve: resolve, reject: reject };
});
setTimeout(function() {
firstResolver.reject("Foo");
}, 0);
setTimeout(function() {
secondResolver.resolve(true);
}, 5000);
var all_promise = RSVP.all([first, second]);
setTimeout(function() {
assert(first.isRejected);
assert.equal(first.rejectedReason, "Foo");
assert(second.isRejected);
assert(second.rejectedReason instanceof RSVP.CancellationError);
done();
}, 20);
});
}); });
describe("RSVP.any", function() { describe("RSVP.any", function() {
...@@ -895,6 +925,36 @@ describe("RSVP extensions", function() { ...@@ -895,6 +925,36 @@ describe("RSVP extensions", function() {
done(); done();
}, 20); }, 20);
}); });
specify('cancel the array of promise as soon as rejected', function(done) {
var firstResolver, secondResolver;
var first = new RSVP.Promise(function(resolve, reject) {
firstResolver = { resolve: resolve, reject: reject };
});
var second = new RSVP.Promise(function(resolve, reject) {
secondResolver = { resolve: resolve, reject: reject };
});
setTimeout(function() {
firstResolver.reject("Foo");
}, 0);
setTimeout(function() {
secondResolver.resolve(true);
}, 5000);
var all_promise = RSVP.any([first, second]);
setTimeout(function() {
assert(first.isRejected);
assert.equal(first.rejectedReason, "Foo");
assert(second.isRejected);
assert(second.rejectedReason instanceof RSVP.CancellationError);
done();
}, 20);
});
}); });
describe("RSVP.reject", function(){ describe("RSVP.reject", function(){
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment