Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement promise.finally (closes #116) #117

Merged
merged 1 commit into from
Aug 31, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
});
});
});