diff --git a/modules/to-callback/index.ts b/modules/to-callback/index.ts index 98ab6d7..05d8ee8 100644 --- a/modules/to-callback/index.ts +++ b/modules/to-callback/index.ts @@ -23,8 +23,7 @@ function toCallback(value: IOptionalPromise, callback: (error?: any, va .resolve(value) .then((value) => { return callback(null, value); - }) - .catch(callback); + }, callback); } export default toCallback; diff --git a/test/to-callback.spec.ts b/test/to-callback.spec.ts index 26cdbeb..200a3ee 100644 --- a/test/to-callback.spec.ts +++ b/test/to-callback.spec.ts @@ -1,17 +1,48 @@ import toCallback from '@promises/to-callback'; describe('toCallback', () => { + it('should be reject on callback throw error', () => { + let promise: Promise = Promise.resolve('foo'); + return toCallback(promise, (error: any, result: string) => { + throw 'reject'; + }).then(() => { + throw 'resolve'; + }).catch((error: string) => { + expect(error).toBe('reject'); + }); + }); + + it('should be resolve promise from callback return', () => { + return toCallback(0, (error: any, num: number) => { + return Promise.resolve(++num); + }).then((num: number) => { + expect(num).toBe(1); + }); + }); + + it('should be reject promise from callback return', () => { + return toCallback(0, (error: any, num: number) => { + return Promise.reject(++num); + }).then(() => { + throw 'resolve'; + }).catch((num: number) => { + expect(num).toBe(1); + }); + }); + it('should be return result to callback', () => { - let promise: any = Promise.resolve('foo'); + let promise: Promise = Promise.resolve('foo'); return toCallback(promise, (error: any, result: string) => { + expect(error).toBeNull(); expect(result).toBe('foo'); }); }); it('should be return error to callback', () => { - let promise: any = Promise.reject('foo'); - return toCallback(promise, (error: any) => { + let promise: Promise = Promise.reject('foo'); + return toCallback(promise, (error: string, result: any) => { expect(error).toBe('foo'); + expect(result).toBeUndefined(); }); }); });