diff --git a/README.md b/README.md index 5433fc8..3a00b19 100644 --- a/README.md +++ b/README.md @@ -15,6 +15,7 @@ Javascript utility functions for web development #### Utility Functions * [async-map](https://thalesrc.github.io/js-utils/modules/_async_map_.html) +* [async-try-catch](https://thalesrc.github.io/js-utils/modules/_async_try_catch_.html) * [clone](https://thalesrc.github.io/js-utils/modules/_clone_.html) * [compact](https://thalesrc.github.io/js-utils/modules/_compact_.html) * [debounce](https://thalesrc.github.io/js-utils/modules/_debounce_.html) diff --git a/package.json b/package.json index 64f8c48..d3aafde 100644 --- a/package.json +++ b/package.json @@ -11,6 +11,7 @@ "array", "async", "map", + "try catch", "compact", "difference", "intersecion", diff --git a/src/as-proto/promise-revert.spec.ts b/src/as-proto/promise-revert.spec.ts index f9930d1..2c2fa15 100644 --- a/src/as-proto/promise-revert.spec.ts +++ b/src/as-proto/promise-revert.spec.ts @@ -1,7 +1,7 @@ import { expect } from 'chai'; import 'mocha'; -import "./promise-revert"; +import './promise-revert'; class CustomError { constructor(public message: string) {} @@ -9,22 +9,22 @@ class CustomError { describe('Promise Revert Proto Function', () => { it('should throw error when its resolved', done => { - Promise.resolve("foo") + Promise.resolve('foo') .revert() .then(() => { - throw new CustomError("bar"); + throw new CustomError('bar'); }) .catch(err => { - expect(err).to.eq("foo"); + expect(err).to.eq('foo'); done(); }); }); it('should resolve when its rejected', done => { - Promise.reject("foo") + Promise.reject('foo') .revert() .then(res => { - expect(res).to.eq("foo"); + expect(res).to.eq('foo'); done(); }); }); diff --git a/src/as-proto/promise-revert.ts b/src/as-proto/promise-revert.ts index 5171b52..21fbadc 100644 --- a/src/as-proto/promise-revert.ts +++ b/src/as-proto/promise-revert.ts @@ -1,4 +1,4 @@ -import { revertPromise } from "../revert-promise"; +import { revertPromise } from '../revert-promise'; declare global { export interface Promise { @@ -31,4 +31,4 @@ declare global { Promise.prototype.revert = function(this: Promise): Promise { return revertPromise(this); -} +}; diff --git a/src/as-proto/promise-try-catch.spec.ts b/src/as-proto/promise-try-catch.spec.ts new file mode 100644 index 0000000..412975f --- /dev/null +++ b/src/as-proto/promise-try-catch.spec.ts @@ -0,0 +1,26 @@ +import { expect } from 'chai'; +import 'mocha'; + +import './promise-try-catch'; + +describe('Promise Try Catch Proto Function', () => { + it('should resolve when its resolved', done => { + Promise.resolve('foo') + .tryCatch() + .then(([error, result]) => { + expect(error).to.eq(null); + expect(result).to.eq('foo'); + done(); + }); + }); + + it('should reject when its rejected', done => { + Promise.reject('foo') + .tryCatch() + .then(([error, result]) => { + expect(error).to.eq('foo'); + expect(result).to.eq(null); + done(); + }); + }); +}); diff --git a/src/as-proto/promise-try-catch.ts b/src/as-proto/promise-try-catch.ts new file mode 100644 index 0000000..974b614 --- /dev/null +++ b/src/as-proto/promise-try-catch.ts @@ -0,0 +1,34 @@ +import { asyncTryCatch } from '../async-try-catch'; + +declare global { + export interface Promise { + /** + * #### Promise Try Catch + * + * Merges result and error in the same callback + * + * * * * + * Example: + * ```typescript + * import "@thalesrc/js-utils/as-proto/promise-try-catch"; + * + * async function fooFunction() { + * const [error, result] = await anAsyncCall().tryCatch(); + * + * if (error) { + * // handle error + * } + * + * // do stuff + * } + * + * ``` + * * * * + */ + tryCatch(): Promise<[E, T]>; + } +} + +Promise.prototype.tryCatch = function(this: Promise): Promise<[E, T]> { + return asyncTryCatch(this); +}; diff --git a/src/async-try-catch.spec.ts b/src/async-try-catch.spec.ts new file mode 100644 index 0000000..90697d3 --- /dev/null +++ b/src/async-try-catch.spec.ts @@ -0,0 +1,25 @@ +import 'mocha'; +import { expect } from 'chai'; +import { asyncTryCatch } from './async-try-catch'; + +describe('AsyncTryCatch Function', () => { + it('should resolve if promise resolved', done => { + const foo = Promise.resolve('foo'); + + asyncTryCatch(foo).then(([err, res]) => { + expect(err).to.eq(null); + expect(res).to.eq('foo'); + done(); + }); + }); + + it('should reject if promise rejected', done => { + const foo = Promise.reject('error'); + + asyncTryCatch(foo).then(([err, res]) => { + expect(err).to.eq('error'); + expect(res).to.eq(null); + done(); + }); + }); +}); diff --git a/src/async-try-catch.ts b/src/async-try-catch.ts new file mode 100644 index 0000000..6f5cc09 --- /dev/null +++ b/src/async-try-catch.ts @@ -0,0 +1,34 @@ +/** + * #### Async Try Catch + * + * Merges result and error in the same callback + * + * * * * + * Example: + * ```typescript + * import { asyncTryCatch } "@thalesrc/js-utils"; + * + * async function fooFunction() { + * const promise = anAsyncCall(); + * const [error, result] = await asyncTryCatch(promise); + * + * if (error) { + * // handle error + * } + * + * // do stuff + * } + * + * ``` + * * * * + * @param promise Promise to try + * @returns Error and result array + */ +export async function asyncTryCatch(promise: Promise): Promise<[E, T]> { + try { + const result = await promise; + return [null, result]; + } catch (error) { + return [error, null]; + } +} diff --git a/src/index.ts b/src/index.ts index a6e46aa..389be5f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,19 +1,20 @@ -export * from "./async-map"; -export { clone } from "./clone"; -export * from "./compact"; -export { debounce } from "./debounce"; -export * from "./defer"; -export { difference } from "./difference"; -export * from "./function-of"; -export { intersection } from "./intersection"; -export * from "./is-falsy"; -export * from "./is-truthy"; -export * from "./min-max"; -export * from "./noop"; -export * from "./open-promise"; -export * from "./promise-timeout"; -export * from "./remove"; -export * from "./replace"; -export { revertPromise } from "./revert-promise"; -export * from "./unique-id"; -export * from "./uniquify"; +export * from './async-map'; +export * from './async-try-catch'; +export { clone } from './clone'; +export * from './compact'; +export { debounce } from './debounce'; +export * from './defer'; +export { difference } from './difference'; +export * from './function-of'; +export { intersection } from './intersection'; +export * from './is-falsy'; +export * from './is-truthy'; +export * from './min-max'; +export * from './noop'; +export * from './open-promise'; +export * from './promise-timeout'; +export * from './remove'; +export * from './replace'; +export { revertPromise } from './revert-promise'; +export * from './unique-id'; +export * from './uniquify'; diff --git a/tslint.json b/tslint.json index 5b3f5dc..909e360 100644 --- a/tslint.json +++ b/tslint.json @@ -31,7 +31,7 @@ ], "quotemark": [ true, - "double", + "single", "avoid-escape" ], "semicolon": [true, "always"],