mirror of
https://github.com/sern-handler/handler
synced 2026-06-26 09:42:15 +00:00
* remove tsresultses * remove test since it uses external api * opt in for simpler * add more debug information Signed-off-by: Jacob Nguyen <76754747+jacoobes@users.noreply.github.com> * add more debug information Signed-off-by: Jacob Nguyen <76754747+jacoobes@users.noreply.github.com> * clean up if else --------- Signed-off-by: Jacob Nguyen <76754747+jacoobes@users.noreply.github.com>
21 lines
715 B
TypeScript
21 lines
715 B
TypeScript
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)); }
|
|
}
|