npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

use-try

v1.0.1

Published

Removing unnecessary try-catch-finally blocks.

Readme

use-try

Clean up your code by removing those ugly try-catch blocks!


About

When working with a codebase where methods are expected to throw errors, it's common to clutter your logic with try-catch blocks, leading to poor code design and maintainability.

use-try solves this problem by abstracting the try-catch logic into a separate method, allowing you to handle errors effectively while still accessing the return value of methods that may throw errors.

This approach helps clean up your code, making it more readable and maintainable.

Installation

To install, run the following command:

npm install --save use-try

Usage

Importing the Package

JavaScript (all versions)

const { useTry, useTryAsync } = require("use-try");

TypeScript or ES6+

import { useTry, useTryAsync } from "use-try";

Example: Synchronous Task

Here's how you can use useTry to handle synchronous tasks that may throw errors:

// Synchronous task example
const syncTask = () => {
  const result = 2 + 2;
  if (result !== 4) {
    throw new Error("The result is invalid");
  }
  return result;
};

// Using useTry to execute the task
const result = useTry(syncTask, "Error executing the synchronous task");

if (result.success) {
  console.log("Task executed successfully:", result.data); // Output: 4
} else {
  console.error("Error:", result.error); // Logs the error if it fails
}

In this example:

  • useTry handles the task and returns a Result<T> object with either the success or error message.

Example: Asynchronous Task

For asynchronous tasks, use useTryAsync to handle errors:

// Asynchronous task example
const asyncTask = async () => {
  const response = await fetch("https://api.example.com/data");
  if (!response.ok) {
    throw new Error("Failed to fetch data");
  }
  return response.json();
};

// Using async/await to execute the asynchronous task
async function executeAsyncTask() {
  const result = await useTryAsync(asyncTask);

  if (result.success) {
    console.log("Asynchronous task executed successfully:", result.data);
  } else {
    console.error("Error:", result.error); // Logs the error if it fails
  }
}

In this case:

  • useTryAsync works similarly but handles asynchronous tasks by returning a Promise<Result<T>>.

API

useTry<T>(task: Task<T>, errorMessage?: string): Result<T>

Tries to execute a synchronous task and returns the result or an error.

  • task: A synchronous function to be executed.
  • errorMessage (optional): Custom error message if the task fails.

Return (Result<T>):

  • success: A boolean value indicating whether the task succeeded (true) or failed (false).
  • data: The result of the task (optional). This field will be present if the task succeeds and will contain the value returned by the task.
  • error: An error message (optional). This field will be present if the task fails and will contain a description of the error.

useTryAsync<T>(task: Task<T>, errorMessage?: string): Promise<Result<T>>

Tries to execute an asynchronous task and returns a Promise with the result or an error.

  • task: A synchronous function or a Promise to be executed.
  • errorMessage (optional): Custom error message if the task fails.

Return (Promise<Result<T>>):

  • success: A boolean value indicating whether the asynchronous task succeeded.
  • data: The value returned by the asynchronous task (if it succeeds).
  • error: An error message, if the asynchronous task fails.

License

This project is licensed under the MIT License. See the LICENSE file for more details