Skip to main content
Module

x/fun/mod.ts>fn_either.tryCatch

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

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>