Skip to main content
Module

x/fun/async_either.ts>tryCatch

A collection of algebraic data types, lenses, and schemables based on a light weight higher kinded type implementation. Written for deno.
Go to Latest
function tryCatch
import { tryCatch } from "https://deno.land/x/fun@v.2.0.0-alpha.11/async_either.ts";

Wraps a Async of A in a try-catch block which upon failure returns B instead. Upon success returns a Right and Left for a failure.

Examples

Example 1

import * as TE from "./async_either.ts";
import * as E from "./either.ts";

const tryFetch = TE.tryCatch(
  fetch,
  (error, args) => ({ message: "Fetch Error", error, args })
);

const result1 = await tryFetch("blah")(); // Left(ErrorStruct)
const result2 = await tryFetch("https://deno.land/")(); // Right(*Deno Website*)

Type Parameters

AS extends unknown[]
A
B

Parameters

fasr: (...as: AS) => A | PromiseLike<A>
onThrow: (e: unknown, as: AS) => B

Returns

(...as: AS) => AsyncEither<B, A>