Skip to main content
Module

x/eitherway/mod.ts>Result.liftFallible

Yet Another Option and Result Implementation - providing safe abstractions for fallible flows inspired by F# and Rust
Latest
function Result.liftFallible
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

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);

Type Parameters

Args extends unknown[]
R
E
optional
T = R

Parameters

fn: (...args: Args) => R
errMapFn: (e: unknown) => E
optional
ctor: (arg: R) => Result<T, E> = [UNSUPPORTED]

Returns

(...args: Args) => Result<T, E>