tryor

v0.1.2
return fn() or default value (in case of exception)
try catch default

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

    var config = tryor(function() {
        return JSON.parse(String(fs.readFileSync("config.json")));
    }, {});

    // instead of
    var config = {};
    try {
        config = JSON.parse(String(fs.readFileSync("config.json")));
    } catch (e) {
    }

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>
npm i tryor

Metadata

  • MIT
  • Whatever
  • Olov Lassus
  • released 4/22/2013

Downloads

Maintainers