Skip to main content
Module

x/fun/mod.ts>fn.tryThunk

A collection of algebraic data types, lenses, and schemables based on a light weight higher kinded type implementation. Written for deno.
Go to Latest
function fn.tryThunk
import { fn } from "https://deno.land/x/fun@v2.0.0-alpha.6/mod.ts";
const { tryThunk } = fn;

Wrap a thunk (a Fn that takes no arguments) in a try catch block, using an onThrow function (that should itself never throw) to handle a default case should the original function throw. This is useful for wrapping functions that might throw.

Examples

Example 1

import * as F from "./fn.ts";

const getZero = (): number => {
  if (Math.random() > 0.5) {
    throw new Error("Too high!");
  }
  return 0;
}

const result = F.tryThunk(getZero, () => 0); // 0

Parameters

ua: Fn<void, A>
onThrow: Fn<E, A>