try-expression
v1.0.0
Published
JavaScript's try statement as an expression
Downloads
3
Readme
try-expression
JavaScript's try statement as an expression.
Install
npm install --save try-expression
Use
Try some code out and deal with an error, like try-catch
:
import tryy from "try-expression";
const result = tryy(
() => doRiskyThing(),
error => {
logError(error);
return 'Sorry!';
}
); // => 'Sorry!'
Fall back to a value if there's an error:
import tryy from "try-expression";
const config = tryy(
() => loadConfig(),
{ squibbles: 4 }
); // => { squibbles: 4 }
Run some code to clean up, like try-catch-finally
:
import tryy from "try-expression";
const result = tryy(
() => ['Success', readFile()],
err => ['Failure', err],
() => closeFile()
); // => ['Failure', err]
Note that, to avoid confusing JavaScript behaviour you cannot return a value from finally
.
Development
$ npm install
$ npm test
Commit messages should follow the Angular commit message guidelines.
Release
This repository uses semantic-release. Changes will automatically be released to npm.