Skip to main content
Module

x/nameable/deps.ts>tryCatch

Cross-check whether it can be used as a package name or domain name
Latest
variable tryCatch
import { tryCatch } from "https://deno.land/x/nameable@v1.1.0-beta.4/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