@el3um4s/to-try
TypeScript icon, indicating that this package has built-in type declarations

1.0.1 • Public • Published

To Try

Wrapper for error handling without try-catch

Based on:

Install and use the package

To use the package in a project:

npm i @el3um4s/to-try

and then in a file:

import { toTry, toTryAsync } from "@el3um4s/to-try";

// Without a custom error handler
const [result, error] = toTry(() => myThrowableMethod());

// With a custom error handler
const [res, err] = toTry(
  () => myThrowableMethod(),
  error => {
    console.log(error);
  }
);

// Handle methods that return a Promise without a custom error handler
const [res2, err2] = await toTryAsync(() => myAsyncThrowableMethod());

// Handle methods that return a Promise with a custom error handler
const [res3, err3] = await toTryAsync(
  () => myAsyncThrowableMethod(),
  error => {
    console.log(error);
  }
);

// Use result
if (error || err || err2 || err3) {
  // Show error alert
}

sendMyResultToMethod(result);

Package Sidebar

Install

npm i @el3um4s/to-try

Weekly Downloads

25

Version

1.0.1

License

MIT

Unpacked Size

8.36 kB

Total Files

11

Last publish

Collaborators

  • el3um4s