Skip to main content
Module

x/core_fn/deps.ts>tryCatch

A collection of built-in object method and property as currying function
Latest
variable tryCatch
import { tryCatch } from "https://deno.land/x/core_fn@v1.0.0-beta.16/deps.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