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/sync_either.ts";

Wrap a function that can throw in a try/catch block, returning a SyncEither.

@example
import { tryCatch } from "./sync_either.ts";

const riskyFunction = () => {
  if (Math.random() > 0.5) throw new Error("Random failure");
  return "Success";
};

const safe = tryCatch(riskyFunction, (e) => `Error: ${e}`);
const result = safe(); // Either Right("Success") or Left("Error: ...")
function tryCatch<A = never, B = never>(fa: () => A, onError: (error: unknown) => B): SyncEither<B, A>;
§
tryCatch<A = never, B = never>(fa: () => A, onError: (error: unknown) => B): SyncEither<B, A>
[src]

§Type Parameters

§
A = never
[src]
§
B = never
[src]

§Parameters

§
fa: () => A
[src]
§
onError: (error: unknown) => B
[src]

§Return Type