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@v2.0.0-alpha.6/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.

import { assertEquals } from "https://deno.land/std/testing/asserts.ts";
import * as TE from "./async_either.ts";
import * as E from "./either.ts";

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

const t1 = await _fetch("blah")();
assertEquals(t1.tag, "Left");

const t2 = await _fetch("https://deno.land/")();
assertEquals(t2.tag, "Right");

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>