Skip to content

Commit

Permalink
feat: add Promise.finally() (#388)
Browse files Browse the repository at this point in the history
Co-authored-by: joshLong145 <[email protected]>
Co-authored-by: Bean <[email protected]>
  • Loading branch information
3 people authored Aug 30, 2024
1 parent cdb5861 commit 0367df6
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,8 @@ The function `Pool.exec` and the proxy functions all return a `Promise`. The pro
Get the result of the promise once resolve.
- `Promise.catch(fn: Function<error: Error>) : Promise<any, Error>`<br>
Get the error of the promise when rejected.
- `Promise.finally(fn: Function<void>)`<br>
Logic to run when the Promise either `resolves` or `rejects`
- `Promise.cancel() : Promise<any, Error>`<br>
A running task can be cancelled. The worker executing the task is enforced to terminate immediately.
The promise will be rejected with a `Promise.CancellationError`.
Expand Down
20 changes: 20 additions & 0 deletions src/Promise.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,26 @@ Promise.prototype.always = function (fn) {
return this.then(fn, fn);
};

/**
* Execute given callback when the promise either resolves or rejects.
* Same semantics as Node's Promise.finally()
* @param {Function} fn
* @returns {Promise} promise
*/
Promise.prototype.finally = function (fn) {
var me = this;
const res = function() {
return me;
}
const final = function() {
return new Promise(function (resolve) {
resolve(fn())
}).then(res);
};

return this.then(final, final);
}

/**
* Create a promise which resolves when all provided promises are resolved,
* and fails when any of the promises resolves.
Expand Down
69 changes: 69 additions & 0 deletions test/Promise.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,75 @@ describe ('Promise', function () {
});
});

describe('finally', function () {
it('should call finally when resolved', function(done) {
var isFullfilled = false;
var finallyRan = false;
var p = new Promise(function(resolve, _reject) {
resolve(1);
}).then(function(value) {
isFullfilled = true;
}).finally(function(value) {
assert.ok(isFullfilled, "should call finally after resolve");
assert.equal(value, undefined);
finallyRan = true;
}).then(function() {
assert.ok(finallyRan, "finallyRan should be called");

done();
});

assert.strictEqual(p.resolved, true);
assert.strictEqual(p.rejected, false);
assert.strictEqual(p.pending, false);
});

it('should call finally when rejected and error is not returned', function(done) {
var isFullfilled = false;
var finallyRan = false;
var p = new Promise(function(_resolve, reject) {
reject(new Error('An error has occured'));
}).catch(function(_err) {
isFullfilled = true;
// dont return the error so the promise doesnt reject and the chain can continue
}).finally(function(value) {
assert.ok(isFullfilled, "should call finally after reject");
assert.equal(value, undefined);
finallyRan = true;
}).then(function() {
assert.ok(finallyRan, "finallyRan should be called");

done();
});

assert.strictEqual(p.resolved, false);
assert.strictEqual(p.rejected, true);
assert.strictEqual(p.pending, false);
});

it('should continue promsie chain from finally if not rejected', function(done) {
var isFullfilled = false;
var finallyRan = false;
var p = new Promise(function(resolve, _reject) {
resolve();
}).then(function () {
isFullfilled = true;
}).finally(function(value) {
assert.ok(isFullfilled, "should call finally after resolve");
assert.equal(value, undefined);
finallyRan = true;
});

return p.then(function() {
assert.ok(finallyRan, 'finallyRan should be true');
assert.strictEqual(p.resolved, true);
assert.strictEqual(p.rejected, false);
assert.strictEqual(p.pending, false);
done();
});
});
});

describe('status', function () {
it('should have correct status before and after being resolved', function (done) {
var p = new Promise(function (resolve, reject) {
Expand Down

0 comments on commit 0367df6

Please sign in to comment.