Skip to main content
Module

x/eitherway/mod.ts>Result.lift

Yet Another Option and Result Implementation - providing safe abstractions for fallible flows inspired by F# and Rust
Latest
function Result.lift
import { Result } from "https://deno.land/x/eitherway@0.10.0/mod.ts";
const { lift } = Result;

Use this to lift a function into a Result context, by composing the wrapped function with a Result constructor.

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 powerOfTwo(n: number): number {
  return Math.pow(2, n);
}

const lifted = Result.lift(powerOfTwo);

const res = Ok(2).andThen(lifted);

assert(res.isOk() === true);
assert(res.unwrap() === 4);

Type Parameters

Args extends unknown[]
R
optional
T = R
optional
E = never

Parameters

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

Returns

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