What is tryor?
The tryor npm package is a utility that allows you to execute a function and return a default value if the function throws an error. This can be particularly useful for handling optional or non-critical operations where you want to ensure that your application continues to run smoothly even if an error occurs.
What are tryor's main functionalities?
Basic Usage
This feature demonstrates the basic usage of the tryor package. It attempts to parse a JSON string and returns a default value if parsing fails.
const tryor = require('tryor');
const result = tryor(() => JSON.parse('{"key": "value"}'), {});
console.log(result); // Output: { key: 'value' }
const resultWithError = tryor(() => JSON.parse('invalid json'), {});
console.log(resultWithError); // Output: {}
Custom Error Handling
This feature shows how you can use tryor to handle custom errors. If the function throws an error, the default value is returned.
const tryor = require('tryor');
const result = tryor(() => {
throw new Error('Something went wrong');
}, 'default value');
console.log(result); // Output: 'default value'
Asynchronous Functions
This feature demonstrates how tryor can be used with asynchronous functions. If the async function throws an error, the default value is returned.
const tryor = require('tryor');
async function asyncFunction() {
return await tryor(async () => {
throw new Error('Async error');
}, 'default async value');
}
asyncFunction().then(result => console.log(result)); // Output: 'default async value'
Other packages similar to tryor
try-catch
The try-catch package provides a similar functionality to tryor by allowing you to execute a function and catch any errors that occur. However, try-catch focuses more on providing a try-catch block as a function, whereas tryor is more about returning a default value in case of an error.
safe-eval
The safe-eval package allows you to safely evaluate JavaScript code without risking security issues. While it is not exactly the same as tryor, it provides a way to handle potentially unsafe operations in a controlled manner. It is more focused on evaluating code safely rather than handling errors and returning default values.
promise-try
The promise-try package is designed to handle promises and asynchronous functions in a way that catches errors and allows for default values or fallback operations. It is similar to tryor in that it provides a way to handle errors gracefully, but it is more focused on promises and async/await patterns.
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")));
}, {});
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>