Skip to main content
Using Deno in production at your company? Earn free Deno merch.
Give us feedback
Module

x/eitherway/lib/async/mod.ts>Task.liftFallible

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

Use this lift a function into a Task context, by composing the wrapped function with a Result constructor and an error mapping function.

If no constructor is provided, Ok is used as a default.

This higher order function is especially useful to intergrate 3rd party code into your Task pipelines.

Examples

Example 1

import { Err, Ok, Result, Task } from "https://deno.land/x/eitherway/mod.ts";

async function toSpecialString(s: string): Promise<string> {
  if (s.length % 3 === 0) return s;
  throw TypeError("Not confomrming to schema");
}

function toTypeError(e: unknown): TypeError {
  if (e instanceof TypeError) return e;
  return TypeError("Unexpected error", { cause: e });
}

const lifted = Task.liftFallible(toSpecialString, toTypeError);

const task: Task<string, TypeError> = Task.succeed("abcd").andThen(lifted);

Type Parameters

Args extends unknown[]
R
E
optional
T = R

Parameters

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

Returns

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