Skip to main content
Module

x/eitherway/mod.ts>Option.liftFallible

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

Same as Option.lift but with a safety net.

Use this if the function to be lifted might throw. In case of an exception, None is returned.

Examples

Example 1

import { assert } from "./assert.ts";
import { Option, None, Some } from "./option.ts";

function fallible(input: number): number {
  if (input === 42) return input;
  throw TypeError("Not even!");
}

const lifted = Option.liftFallible(fallible, Option.fromCoercible);

const maybe = Option.from(42).andThen(lifted);

assert(maybe.isSome() === true);
assert(maybe.unwrap() === 42);

Type Parameters

Args extends Readonly<unknown[]>
R1
optional
R2 = NonNullish<R1>

Parameters

fn: (...args: Args) => R1
optional
ctor: (arg: R1) => Option<R2> = [UNSUPPORTED]