import { fn_either } from "https://deno.land/x/fun@v.2.0.0-alpha.11/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
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);