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.fromFallible

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

Use this to construct a Task<T, E> from the return value of a fallible function.

Examples

Example 1

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

async function rand(): Promise<number> {
  throw new TypeError("Oops");
}

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

const task: Task<number, TypeError> = Task.fromFallible(
  rand,
  toTypeError,
)

Parameters

fn: () => T | PromiseLike<T>
errorMapFn: (reason: unknown) => E