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:
- a function to execute
- (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