-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #24 from thalesrc/feature/promise-try-catch
feat(Promise): Async try catch function completed
- Loading branch information
Showing
10 changed files
with
150 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ | |
"array", | ||
"async", | ||
"map", | ||
"try catch", | ||
"compact", | ||
"difference", | ||
"intersecion", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,7 +31,7 @@ | |
], | ||
"quotemark": [ | ||
true, | ||
"double", | ||
"single", | ||
"avoid-escape" | ||
], | ||
"semicolon": [true, "always"], | ||
|