34 lines
772 B
JavaScript
Executable File
34 lines
772 B
JavaScript
Executable File
export function ok(value) {
|
|
return { _tag: 'ok', value };
|
|
}
|
|
export function err(error) {
|
|
return { _tag: 'error', error };
|
|
}
|
|
/**
|
|
* Checks whether a value is an `Ok`.
|
|
* Handy with TypeScript guards
|
|
*/
|
|
export function isOk(result) {
|
|
return result._tag === 'ok';
|
|
}
|
|
/**
|
|
* Checks whether a value is an `Err`.
|
|
* Handy with TypeScript guards
|
|
*/
|
|
export function isErr(either) {
|
|
return either._tag === 'error';
|
|
}
|
|
/**
|
|
* Convert a `Promise<T>` into a `Promise<Result<Error, T>>`,
|
|
* therefore catching the errors and being able to handle them explicitly
|
|
*/
|
|
export async function safeAsync(promise) {
|
|
try {
|
|
const value = await promise;
|
|
return ok(value);
|
|
}
|
|
catch (e) {
|
|
return err(e);
|
|
}
|
|
}
|
|
//# sourceMappingURL=Result.js.map
|