Skip to content
master
Switch branches/tags
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
 
 
 
 
 
 
 
 
 
 
 
 
 
 

try-expression

semantic-release Build Status

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.

About

JavaScript's try statement as an expression

Resources

License

Packages

No packages published