jack e1a2621c78 update | vor 2 Jahren | |
---|---|---|
.. | ||
LICENSE | vor 2 Jahren | |
README.md | vor 2 Jahren | |
package.json | vor 2 Jahren | |
tryor.js | vor 2 Jahren |
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.
// 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.
Install using npm
npm install tryor
var tryor = require("tryor");
Clone the repo and include it in a script tag
git clone https://github.com/olov/tryor.git
<script src="tryor/tryor.js"></script>