Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

tryor

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

tryor

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

  • 0.1.2
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
174K
decreased by-13.45%
Maintainers
1
Weekly downloads
 
Created

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

Keywords

FAQs

Package last updated on 22 Apr 2013

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc