JSPM

tryor

0.1.2
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 268931
  • Score
    100M100P100Q164946F
  • License MIT

return fn() or default value (in case of exception)

Package Exports

  • tryor

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (tryor) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

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>