Skip to main content

Wraps any method with a try/catch block.

Works with
This package works with Bun
This package works with Cloudflare Workers
This package works with Node.js
This package works with Deno
This package works with Browsers
JSR Score
64%
Published
3 months ago

@utils/try-catch

Wraps a method with a try/catch block and standardises the result in the following format:

{
  result: any | null
  error: typeof Error | null
}

Example usage

import { tryCatch } from '@utils/try-catch'

const myMethod = () => {
  // Do something which could throw here....
}

const methodResult = tryCatch(myMethod)

if (methodResult.error) {
  // An error occured
  console.error(methodResult.error)
}

console.log(methodResult.result)

Add Package

deno add @utils/try-catch

Import symbol

import * as mod from "@utils/try-catch";

Add Package

npx jsr add @utils/try-catch

Import symbol

import * as mod from "@utils/try-catch";

Add Package

yarn dlx jsr add @utils/try-catch

Import symbol

import * as mod from "@utils/try-catch";

Add Package

pnpm dlx jsr add @utils/try-catch

Import symbol

import * as mod from "@utils/try-catch";

Add Package

bunx jsr add @utils/try-catch

Import symbol

import * as mod from "@utils/try-catch";