Skip to main content

Provides a utility function to handle async and sync functions with try-catch, returning an object with either data or an error. Simplifies error handling in Deno applications.

This package works with Node.js, Deno, BrowsersIt is unknown whether this package works with Cloudflare Workers, Bun
It is unknown whether this package works with Cloudflare Workers
This package works with Node.js
This package works with Deno
It is unknown whether this package works with Bun
This package works with Browsers
JSR Score
100%
Published
2 months ago (0.1.2)

Examples

Example with a Function Returning Data

import { withTryCatch } from "@aissadev1/with-try-catch";

function returnData(): string {
  return "This is the data";
}

const result = await withTryCatch(returnData);
if (result.error) {
  console.error("Error:", result.error.message);
} else {
  console.log("Data:", result.data); // Output: "This is the data"
}

Example with a Function Throwing an Error

import { withTryCatch } from "@aissadev1/with-try-catch";

function throwError(): string {
  throw new Error("This is an error");
}

const result = await withTryCatch(throwError);
if (result.error) {
  console.error("Error:", result.error.message); // Output: "This is an error"
} else {
  console.log("Data:", result.data);
}

Installation

Add the package using JSR:

deno add jsr:@aissadev1/with-try-catch

or

npx jsr add @aissadev1/with-try-catch

Import

import { withTryCatch } from "@aissadev1/with-try-catch";

Add Package

deno add jsr:@aissadev1/with-try-catch

Import symbol

import * as with_try_catch from "@aissadev1/with-try-catch";

---- OR ----

Import directly with a jsr specifier

import * as with_try_catch from "jsr:@aissadev1/with-try-catch";

Add Package

npx jsr add @aissadev1/with-try-catch

Import symbol

import * as with_try_catch from "@aissadev1/with-try-catch";

Add Package

yarn dlx jsr add @aissadev1/with-try-catch

Import symbol

import * as with_try_catch from "@aissadev1/with-try-catch";

Add Package

pnpm dlx jsr add @aissadev1/with-try-catch

Import symbol

import * as with_try_catch from "@aissadev1/with-try-catch";

Add Package

bunx jsr add @aissadev1/with-try-catch

Import symbol

import * as with_try_catch from "@aissadev1/with-try-catch";