Package Exports
- ok-value-error-reason
- ok-value-error-reason/dist/esm/index.js
- ok-value-error-reason/dist/umd/index.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (ok-value-error-reason) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
ok-value-error-reason
An elegant way to handle exceptions from both synchronous and asynchronous functions.
Usage
By wrapping a function call with over, you get an union types Over (ok-value-error-reason) that represents the result of a function call.
import {
over, // stand for 'ok-value-error-reason'
} from "ok-value-error-reason";
let ans = fnLetCrash(arg1); // => crash
let ans = over(fnLetCrash, arg1); // => ans is an `Over` objectOver object is either a OKValue or ErrorReason object.
export type OKValue<V> = {
ok: true;
value: V;
};
export type ErrorReason<R> = {
ok: false;
reason: R;
};
export type OVER<V, E> = OKValue<V> | ErrorReason<E>;You can use .ok property to narrow types.
// Wrap fnReturnNumber execution so that it will return an Over object.
let ans = over<typeof fnReturnNumber, "EMPTY" | "TOO_LONG">(
fnReturnNumber,
arg1,
);
if (ans.ok) {
// `ans` is an OKValue object
console.log(ans.value); // `.value` is number
} else {
// `ans` is an ErrorReason object
console.error(ans.reason); // `.reason` is "EMPTY" | "TOO_LONG"
}over is also compatible with async functions.
// async
let asyncAns = await over<typeof asyncFnReturnNumber, "EMPTY" | "TOO_LONG">(
asyncFnReturnNumber,
arg1,
);