@shanedaugherty/try-or

1.0.3 • Public • Published

TryOr

How to install

npm install @shanedaugherty/try-or --save

What is it

TryOr is a simple utility function that can be used to use execute code within a supercharged, one-line try catch block.

How does it work

TryOr is just a function that has two parameters:

  1. a function to execute
  2. (optional) either a fallback value, or a fallback function. Default is null.
const result = tryOr(() => mightThrowAnError(), 'hello')

// result is 'hello';
const result = tryOr(
    () => mightThrowAnError(),
    (error) => {
        logError(error);
        return 'hi';
    }
)

// result is 'hi';

See more examples here: https://shanedaugherty.com/posts/a-shortcut-to-the-try-catch-block

Readme

Keywords

none

Package Sidebar

Install

npm i @shanedaugherty/try-or

Weekly Downloads

1

Version

1.0.3

License

ISC

Unpacked Size

10.7 kB

Total Files

16

Last publish

Collaborators

  • shanedaugherty