-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: remove ts-results-es (#366)
* remove tsresultses * remove test since it uses external api * opt in for simpler * add more debug information Signed-off-by: Jacob Nguyen <[email protected]> * add more debug information Signed-off-by: Jacob Nguyen <[email protected]> * clean up if else --------- Signed-off-by: Jacob Nguyen <[email protected]>
- Loading branch information
Showing
10 changed files
with
77 additions
and
101 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
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 was deleted.
Oops, something went wrong.
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,20 @@ | ||
export type Result<Ok, Err> = | ||
| { ok: true; value: Ok } | ||
| { ok: false; error: Err }; | ||
|
||
export const Ok = <Ok>(value: Ok) => ({ ok: true, value } as const); | ||
export const Err = <Err>(error: Err) => ({ ok: false, error } as const); | ||
|
||
export const val = <O, E>(r: Result<O, E>) => r.ok ? r.value : r.error; | ||
export const EMPTY_ERR = Err(undefined); | ||
|
||
/** | ||
* Wrap an async operation that may throw an Error (`try-catch` style) into checked exception style | ||
* @param op The operation function | ||
*/ | ||
export async function wrapAsync<T, E = unknown>(op: () => Promise<T>): Promise<Result<T, E>> { | ||
try { return op() | ||
.then(Ok) | ||
.catch(Err); } | ||
catch (e) { return Promise.resolve(Err(e as E)); } | ||
} |
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
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