Skip to main content
Deno 2 is finally here 🎉️
Learn more
Module

x/fonction/src/tryCatch.ts>tryCatch

A modern practical functional library
Latest
variable tryCatch
import { tryCatch } from "https://deno.land/x/fonction@v2.1.0-beta.4/src/tryCatch.ts";

tryCatch takes two functions, a tryer and a catcher. The returned function evaluates the tryer; if it does not throw, it simply returns the result. If the tryer does throw, the returned function evaluates the catcher function and returns its result.

Examples

Example 1

tryCatch(() => { throw Error('error') }) // Error('error')
tryCatch(() => { throw Error('error') }, 0) // 0
tryCatch(() => { throw Error('error') }, (e: Error) => e.message ) // 'error'

type

<R, E, P = unknown>(tryer: AnyFn<any, R>, catcher?: E | AnyFn<P, E>) => R | E