import { Result } from "https://deno.land/x/eitherway@0.10.0/mod.ts";
const { liftFallible } = Result;
Use this to lift a fallible function into a Result
context, by composing
the wrapped function with a Result
constructor and an error mapping
function.
If no constructor is provided, Ok
is used as default.
This is useful for integrating 3rd party code without the need to manually wrap it.
Examples
Example 1
Example 1
import { assert } from "./assert.ts";
import { Err, Ok, Result } from "./mod.ts";
function toSpecialString(s: string): string {
if (s.length % 3 === 0) return s;
throw TypeError("Not conforming to schema");
}
function toTypeError(e: unknown): TypeError {
if (e instanceof TypeError) return e;
return TypeError("Unexpected error", { cause: e });
}
const lifted = Result.liftFallible(toSpecialString, toTypeError);
const res: Result<string, TypeError> = Ok("abcd").andThen(lifted);
assert(res.isOk() === false);
assert(res.unwrap() instanceof TypeError === true);