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://raw.githubusercontent.com/baetheus/fun/main/option.ts";

Take a function that can throw and wrap it in a try/catch block. Returns a new function that takes the same arguments as the original but returns the original value wrapped in an Option. If the function throws then the new function returns None, otherwise it returns Some.

@example
import * as O from "./option.ts";

function thrower(n: number): number {
  if (n < 0) {
    throw new Error("This number is too small");
  }
  return n;
}

const handler = O.tryCatch(thrower);

const result1 = handler(-1); // None
const result2 = handler(0); // Some(0);
function tryCatch<D extends unknown[], A>(fn: (...d: D) => A): (...d: D) => Option<A>;
§
tryCatch<D extends unknown[], A>(fn: (...d: D) => A): (...d: D) => Option<A>
[src]

§Type Parameters

§
D extends unknown[]
[src]

§Parameters

§
fn: (...d: D) => A
[src]

§Return Type

§
(...d: D) => Option<A>
[src]