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

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

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

const riskyFunction = (n: number) => {
  if (n < 0) throw new Error("Negative number");
  return n * 2;
};

const safe = tryCatch(riskyFunction, (e, args) => 0);

const result1 = await safe(5)(); // 10
const result2 = await safe(-1)(); // 0
function tryCatch<AS extends unknown[], A>(fasr: (...as: AS) => A | PromiseLike<A>, onThrow: (e: unknown, as: AS) => A): (...as: AS) => Async<A>;
§
tryCatch<AS extends unknown[], A>(fasr: (...as: AS) => A | PromiseLike<A>, onThrow: (e: unknown, as: AS) => A): (...as: AS) => Async<A>
[src]

§Type Parameters

§
AS extends unknown[]
[src]

§Parameters

§
fasr: (...as: AS) => A | PromiseLike<A>
[src]
§
onThrow: (e: unknown, as: AS) => A
[src]

§Return Type

§
(...as: AS) => Async<A>
[src]