@shanedaugherty/try-or

A shortcut to the try catch block.

Usage no npm install needed!

<script type="module">
  import shanedaughertyTryOr from 'https://cdn.skypack.dev/@shanedaugherty/try-or';
</script>

README

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