Commit c2973910 authored by Romain Courteaud's avatar Romain Courteaud

adding promise#always

parent f8ce66af
...@@ -145,6 +145,10 @@ Promise.prototype = { ...@@ -145,6 +145,10 @@ Promise.prototype = {
fail: function(fail) { fail: function(fail) {
return this.then(null, fail); return this.then(null, fail);
},
always: function(fail) {
return this.then(fail, fail);
} }
}; };
......
...@@ -1328,3 +1328,64 @@ describe("`cancel` on promise created by then", function () { ...@@ -1328,3 +1328,64 @@ describe("`cancel` on promise created by then", function () {
}); });
}); });
describe("`always` on a promise", function () {
it('should be a function', function () {
var promise = new RSVP.Promise(function (done, fail) {});
assert.equal(typeof promise.always, "function");
});
it('should return a new promise', function () {
var promise = new RSVP.Promise(function (done, fail) {}),
promise2 = promise.always();
assert(promise2 instanceof RSVP.Promise);
});
it('should fulfill the new promise if the parent is fulfilled', function (done) {
var promise = new RSVP.Promise(function (accept, fail) {accept("bar")}),
promise2 = promise.always();
setTimeout(function() {
assert.equal(promise2.isFulfilled, true);
assert.equal(promise2.isRejected, undefined);
assert.equal(promise2.fulfillmentValue, "bar");
done();
}, 20);
});
it('should call the function parameter if the parent is fulfilled',
function (done) {
var promise = new RSVP.Promise(function (accept, fail) {accept("bar")}),
parameter_called = false,
promise2 = promise.always(function () {parameter_called = true});
setTimeout(function() {
assert.equal(parameter_called, true);
done();
}, 20);
});
it('should reject the new promise if the parent is rejected', function (done) {
var promise = new RSVP.Promise(function (accept, fail) {fail("foo")}),
promise2 = promise.always();
setTimeout(function() {
assert.equal(promise2.isFulfilled, undefined);
assert.equal(promise2.isRejected, true);
assert.equal(promise2.rejectedReason, "foo");
done();
}, 20);
});
it('should call the function parameter if the parent is rejected',
function (done) {
var promise = new RSVP.Promise(function (accept, fail) {fail("foo")}),
parameter_called = false,
promise2 = promise.always(function () {parameter_called = true});
setTimeout(function() {
assert.equal(parameter_called, true);
done();
}, 20);
});
});
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