tryor

0.1.2 • Public • Published

tryor.js

Give it a function and a default value. tryor will give you back the return value of the function or, in case the function threw an exception, the default value. Works in node and browsers.

Usage

    // do this
    var config = tryor(function() {
        return JSON.parse(userProvidedConfigString);
    }, {});
 
    // instead of
    var config; // or var config = {}; and empty catch
    try {
        config = JSON.parse(userProvidedConfigString);
    } catch (e) {
        config = {};
    }

Works extra well with ES6-style const variables.

Installation

Node

Install using npm

npm install tryor
var tryor = require("tryor");

Browser

Clone the repo and include it in a script tag

git clone https://github.com/olov/tryor.git
<script src="tryor/tryor.js"></script>

Versions

Current Tags

  • Version
    Downloads (Last 7 Days)
    • Tag
  • 0.1.2
    179,918
    • latest

Version History

  • Version
    Downloads (Last 7 Days)
    • Published
  • 0.1.2
    179,918
  • 0.1.1
    0
  • 0.1.0
    1

Package Sidebar

Install

npm i tryor

Weekly Downloads

149,787

Version

0.1.2

License

MIT

Last publish

Collaborators

  • olov