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