Skip to main content
Deno 2 is finally here 🎉️
Learn more
Module

x/fun/fn_either.ts>tryCatch

A collection of algebraic data types, lenses, and schemables based on a light weight higher kinded type implementation. Written for deno.
Latest
function tryCatch
import { tryCatch } from "https://deno.land/x/fun@v2.0.0/fn_either.ts";

Wrap any function in a try catch block. The returned function will lazily call and handle any throwing of the wrapped function. Non-throwing calls will be returned in a Right, and throwing calls will have their error and arguments passed to the onThrow function before being returned in a Left.

Examples

Example 1

import { tryCatch } from "./fn_either.ts";
import { todo } from "./fn.ts";

const throws = tryCatch(todo<number>, () => "Failed!");
const returns = tryCatch((n: number) => n, () => "Failed!");

const result1 = throws()(0); // Left("Failed!");
const result2 = returns(1)(0); // Right(1);

Type Parameters

D extends unknown[]
B
A

Parameters

ua: (...d: D) => A
onThrow: (e: unknown, d: D) => B

Returns

(...d: D) => FnEither<unknown, B, A>