Skip to content

Commit

Permalink
fix(finally): wrong return after rejection
Browse files Browse the repository at this point in the history
  • Loading branch information
yisraelx committed Feb 27, 2018
1 parent 0288d2c commit ddedc6a
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 11 deletions.
13 changes: 7 additions & 6 deletions modules/finally/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

import Promises from '@promises/core';
import { IOptionalPromise } from '@promises/interfaces';
import next from '@promises/next';
import error from '@promises/error';

/**
* @example
Expand All @@ -25,12 +27,11 @@ import { IOptionalPromise } from '@promises/interfaces';
* });
* ```
*/
function _finally<R>(promise: IOptionalPromise<any>, fn: () => IOptionalPromise<any>): Promises<R> {
let onBoth = (value) => {
let result = fn();
return Promise.resolve(result).then(() => value);
};
return Promises.resolve(promise).then(onBoth, onBoth) as Promises<R>;
function _finally<R>(promise: IOptionalPromise<R>, fn: () => IOptionalPromise<any>): Promises<R> {
return Promises.resolve(promise).then(
(value) => next(fn(), value),
(reason) => error(fn(), reason)
);
}

export default _finally;
Expand Down
5 changes: 3 additions & 2 deletions modules/finally/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
"url": "https://github.com/yisraelx/promises/issues"
},
"dependencies": {
"@promises/core": "^0.2.0",
"@promises/interfaces": "^0.2.0"
"@promises/error": "^0.2.0",
"@promises/interfaces": "^0.2.0",
"@promises/next": "^0.2.0"
}
}
21 changes: 18 additions & 3 deletions test/finally.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,27 @@ describe('finally', () => {

it('should be exec after promise reject and return the data', () => {
let promise = Promises.reject('reject');
let pass = false;
let execFinally = false;
let catchError = false;
return _finally(promise, () => {
pass = true;
expect(catchError).toBeFalsy();
execFinally = true;
}).then(() => {
throw 'resolve';
}).catch((data) => {
expect(data).toBe('reject');
expect(pass).toBeTruthy();
expect(execFinally).toBeTruthy();
catchError = true;
});
});

it('should be reject on throw error', () => {
return _finally('foo', () => {
throw 'finally';
}).then(() => {
throw 'resolve';
}).catch((data) => {
expect(data).toBe('finally');
});
});
});

0 comments on commit ddedc6a

Please sign in to comment.