Skip to content

Commit

Permalink
Merge pull request #24 from thalesrc/feature/promise-try-catch
Browse files Browse the repository at this point in the history
feat(Promise): Async try catch function completed
  • Loading branch information
alisahinozcelik authored Dec 13, 2019
2 parents e9bf40d + 379658c commit f801861
Show file tree
Hide file tree
Showing 10 changed files with 150 additions and 28 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"array",
"async",
"map",
"try catch",
"compact",
"difference",
"intersecion",
Expand Down
12 changes: 6 additions & 6 deletions src/as-proto/promise-revert.spec.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
import { expect } from 'chai';
import 'mocha';

import "./promise-revert";
import './promise-revert';

class CustomError {
constructor(public message: string) {}
}

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();
});
});
Expand Down
4 changes: 2 additions & 2 deletions src/as-proto/promise-revert.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { revertPromise } from "../revert-promise";
import { revertPromise } from '../revert-promise';

declare global {
export interface Promise<T> {
Expand Down Expand Up @@ -31,4 +31,4 @@ declare global {

Promise.prototype.revert = function<T = any>(this: Promise<any>): Promise<T> {
return revertPromise<T>(this);
}
};
26 changes: 26 additions & 0 deletions src/as-proto/promise-try-catch.spec.ts
Original file line number Diff line number Diff line change
@@ -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();
});
});
});
34 changes: 34 additions & 0 deletions src/as-proto/promise-try-catch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { asyncTryCatch } from '../async-try-catch';

declare global {
export interface Promise<T> {
/**
* #### 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<E = any>(): Promise<[E, T]>;
}
}

Promise.prototype.tryCatch = function<T, E>(this: Promise<T>): Promise<[E, T]> {
return asyncTryCatch<T, E>(this);
};
25 changes: 25 additions & 0 deletions src/async-try-catch.spec.ts
Original file line number Diff line number Diff line change
@@ -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();
});
});
});
34 changes: 34 additions & 0 deletions src/async-try-catch.ts
Original file line number Diff line number Diff line change
@@ -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<T, E = any>(promise: Promise<T>): Promise<[E, T]> {
try {
const result = await promise;
return [null, result];
} catch (error) {
return [error, null];
}
}
39 changes: 20 additions & 19 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -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';
2 changes: 1 addition & 1 deletion tslint.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
],
"quotemark": [
true,
"double",
"single",
"avoid-escape"
],
"semicolon": [true, "always"],
Expand Down

0 comments on commit f801861

Please sign in to comment.