From ddedc6ab2f11b34b53ddf2b9eba8e823e5b3e88e Mon Sep 17 00:00:00 2001 From: yisraelx Date: Tue, 27 Feb 2018 20:40:20 +0200 Subject: [PATCH] fix(finally): wrong return after rejection --- modules/finally/index.ts | 13 +++++++------ modules/finally/package.json | 5 +++-- test/finally.spec.ts | 21 ++++++++++++++++++--- 3 files changed, 28 insertions(+), 11 deletions(-) diff --git a/modules/finally/index.ts b/modules/finally/index.ts index c741233..57105e1 100644 --- a/modules/finally/index.ts +++ b/modules/finally/index.ts @@ -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 @@ -25,12 +27,11 @@ import { IOptionalPromise } from '@promises/interfaces'; * }); * ``` */ -function _finally(promise: IOptionalPromise, fn: () => IOptionalPromise): Promises { - let onBoth = (value) => { - let result = fn(); - return Promise.resolve(result).then(() => value); - }; - return Promises.resolve(promise).then(onBoth, onBoth) as Promises; +function _finally(promise: IOptionalPromise, fn: () => IOptionalPromise): Promises { + return Promises.resolve(promise).then( + (value) => next(fn(), value), + (reason) => error(fn(), reason) + ); } export default _finally; diff --git a/modules/finally/package.json b/modules/finally/package.json index 5e1925b..d442b0a 100644 --- a/modules/finally/package.json +++ b/modules/finally/package.json @@ -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" } } diff --git a/test/finally.spec.ts b/test/finally.spec.ts index 5868941..cd1555e 100644 --- a/test/finally.spec.ts +++ b/test/finally.spec.ts @@ -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'); }); }); });