JavaScript's try statement as an expression
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.editorconfig
.gitignore
.travis.yml
LICENSE
README.md
index.js
package.json
yarn.lock

README.md

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.