Hi there! Are you looking for the official Deno documentation? Try docs.deno.com for all your Deno learning needs.

tryCatch

import { tryCatch } from "https://github.gh-proxy.cn/raw.githubusercontent.com/baetheus/fun/main/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.

@example
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);
function tryCatch<D extends unknown[], B, A>(ua: (...d: D) => A, onThrow: (e: unknown, d: D) => B): (...d: D) => FnEither<unknown, B, A>;
§
tryCatch<D extends unknown[], B, A>(ua: (...d: D) => A, onThrow: (e: unknown, d: D) => B): (...d: D) => FnEither<unknown, B, A>
[src]

§Type Parameters

§
D extends unknown[]
[src]

§Parameters

§
ua: (...d: D) => A
[src]
§
onThrow: (e: unknown, d: D) => B
[src]

§Return Type

§
(...d: D) => FnEither<unknown, B, A>
[src]