Utilities that wrap promise or synchronous function to catch the error then return the result and error in consistent tuple format.
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
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);
}
promise
: A promise to be resolved or rejected.errorsToCatch
(optional): An array of error types to catch.
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.
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);
}
callback
: The function to be executed synchronously.errorsToCatch
(optional): An array of error types to catch.
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.