Skip to content

Utilities that wrap promise or synchronous function to catch the error then return the result and error in consistent tuple format.

Notifications You must be signed in to change notification settings

tfkhdyt/with-catch

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

With Catch

Utilities that wrap promise or synchronous function to catch the error then return the result and error in consistent tuple format.

Installation

To use these utilities in your project, you can install via your favorite package manager:

# pnpm
pnpx jsr add @tfkhdyt/with-catch

# npm
npx jsr add @tfkhdyt/with-catch

# bun
bunx jsr add @tfkhdyt/with-catch

# deno
deno add jsr:@tfkhdyt/with-catch

Usage

withCatch

The withCatch function handles a promise and catches specific errors.

import { withCatch } from "@tfkhdyt/with-catch";

// Usage example
const [error, data] = await withCatch(yourPromise, [TypeError, ReferenceError]);

if (error) {
  console.error("Caught an error:", error);
} else {
  console.log("Data received:", data);
}

Parameters

  • promise: A promise to be resolved or rejected.
  • errorsToCatch (optional): An array of error types to catch.

Returns

A tuple where the first element is undefined if no error occurred, and the second is the resolved value of the promise. If an error is caught, the tuple contains the error instance.

withCatchSync

The withCatchSync function executes a synchronous callback and catches specific errors.

import { withCatchSync } from "@tfkhdyt/with-catch";

// Usage example
const [error, result] = withCatchSync(() => {
  // Synchronous operation
  return someValue;
}, [TypeError, ReferenceError]);

if (error) {
  console.error("Caught an error:", error);
} else {
  console.log("Result:", result);
}

Parameters

  • callback: The function to be executed synchronously.
  • errorsToCatch (optional): An array of error types to catch.

Returns

A tuple where the first element is undefined if no error occurred, and the second is the result of the callback. If an error is caught, the tuple contains the error instance.

About

Utilities that wrap promise or synchronous function to catch the error then return the result and error in consistent tuple format.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published