Skip to content

Commit

Permalink
Implement promise.finally (closes #116)
Browse files Browse the repository at this point in the history
  • Loading branch information
dfilatov committed Aug 30, 2018
1 parent 20c7f2a commit 4b03fe9
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 1 deletion.
19 changes: 19 additions & 0 deletions lib/vow.js
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,25 @@ Promise.prototype = /** @lends Promise.prototype */ {
return this.then(cb, cb, ctx);
},

/**
* Adds a resolving reaction (for both fulfillment and rejection). The returned promise will be fullfiled with the same value or rejected with the same reason as the original promise.
*
* @param {Function} onFinalized Callback that will be invoked after the promise has been resolved.
* @param {Object} [ctx] Context of the callback execution
* @returns {vow:Promise}
*/
'finally' : function(onFinalized, ctx) {
var _this = this,
cb = function() {
return onFinalized.call(this);
};
return this
.then(cb, cb, ctx)
.then(function() {
return _this;
});
},

/**
* Adds a progress reaction.
*
Expand Down
61 changes: 61 additions & 0 deletions test/promise.finally.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
module.exports = {
'onFinalized callback should be called without arguments on fulfill' : function(test) {
var defer = Vow.defer();
defer.resolve('ok');
defer.promise().finally(function() {
test.strictEqual(arguments.length, 0);
test.done();
});
},

'onFinalized callback should be called without arguments on reject' : function(test) {
var defer = Vow.defer();
defer.reject('error');
defer.promise().finally(function() {
test.strictEqual(arguments.length, 0);
test.done();
});
},

'returned promise should be fulfilled with value of original promise' : function(test) {
var defer = Vow.defer();
defer.resolve('ok');
defer.promise()
.finally(function() {
return 'finally';
})
.then(function(val) {
test.strictEqual(val, 'ok');
test.done();
});
},

'returned promise should be rejected with value of original promise' : function(test) {
var defer = Vow.defer(),
error = new Error('error');
defer.reject(error);
defer.promise()
.finally(function() {
return new Error('finally');
})
.fail(function(_error) {
test.strictEqual(_error, error);
test.done();
});
},

'returned promise should be rejected with value of returned promise' : function(test) {
var defer = Vow.defer(),
error = new Error('error'),
finallyError = new Error('finally');
defer.reject(error);
defer.promise()
.finally(function() {
return Vow.reject(finallyError);
})
.fail(function(_error) {
test.strictEqual(_error, finallyError);
test.done();
});
}
};
2 changes: 1 addition & 1 deletion test/utils/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ require('nodeunit').reporters.default.run(
require('promises-aplus-tests')(require('./aplus-adapter'), function(err) {
err && process.exit(1);
});
});
});

0 comments on commit 4b03fe9

Please sign in to comment.