Commit bebc30b9 authored by Romain Courteaud's avatar Romain Courteaud

Prevent resolving a promise twice.

parent d6449937
......@@ -78,6 +78,9 @@ var invokeCallback = function(type, promise, callback, event) {
var hasCallback = isFunction(callback),
value, error, succeeded, failed;
if (promise.isFulfilled) { return; }
if (promise.isRejected) { return; }
if (hasCallback) {
try {
value = callback(event.detail);
......@@ -207,6 +210,8 @@ function handleThenable(promise, value) {
function fulfill(promise, value) {
config.async(function() {
if (promise.isFulfilled) { return; }
if (promise.isRejected) { return; }
promise.trigger('promise:resolved', { detail: value });
promise.isFulfilled = true;
promise.fulfillmentValue = value;
......@@ -215,6 +220,8 @@ function fulfill(promise, value) {
function reject(promise, value) {
config.async(function() {
if (promise.isFulfilled) { return; }
if (promise.isRejected) { return; }
promise.trigger('promise:failed', { detail: value });
promise.isRejected = true;
promise.rejectedReason = value;
......
......@@ -1489,6 +1489,50 @@ describe("`cancel` on promise created by then", function () {
assert.equal(typeof promise.cancel, "function");
});
it('should stop parent fulFillment propagation', function (done) {
var promise = new RSVP.resolve(1),
promise2 = promise.then();
promise2.cancel("Foo");
setTimeout(function() {
assert.equal(promise2.isRejected, true);
assert.equal(promise2.isFulfilled, undefined);
assert(promise2.rejectedReason instanceof RSVP.CancellationError);
done();
}, 20);
});
it('should stop parent rejection propagation', function (done) {
var reject_count = 0,
promise = new RSVP.reject(1),
promise2 = promise.fail(function () {reject_count += 1;});
promise2.cancel("Foo");
setTimeout(function() {
assert.equal(promise2.isRejected, true);
assert.equal(promise2.isFulfilled, undefined);
assert(promise2.rejectedReason instanceof RSVP.CancellationError);
assert.equal(reject_count, 1);
done();
}, 20);
});
it('should not be callable twice', function (done) {
var reject_count = 0,
promise = new RSVP.reject(1),
promise2 = promise.fail(function () {reject_count += 1;});
promise2.cancel("Foo");
promise2.cancel("Bar");
setTimeout(function() {
assert.equal(promise2.isRejected, true);
assert.equal(promise2.isFulfilled, undefined);
assert(promise2.rejectedReason instanceof RSVP.CancellationError);
assert.equal(reject_count, 1);
done();
}, 20);
});
it('should cancel the pending fulfilled promise', function (done) {
var cancel_called = false,
promise = new RSVP.Promise(function (resolve) {resolve();}),
......@@ -1804,13 +1848,12 @@ describe("`RSVP.Queue`", function () {
assert(promise instanceof RSVP.Promise);
});
it('should work without `new`', function(done) {
it('should work without `new`', function() {
var promise = RSVP.Queue()
.push(function() { return 'value'; });
promise.then(function(value) {
assert.equal(value, 'value');
done();
});
});
});
......
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