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 🙏

© 2025 – Pkg Stats / Ryan Hefner

tryresult

v2.0.7

Published

A typescript library to get rid of try catches.

Readme

npm bundle size npm npm

📛 TryResult

A tiny typescript library to get rid of try catches, and replace them with result types, inspired by Rust and Go error handling.

preview picture

Under 650 bytes (minified and gzipped) and with no dependencies, TryResult only provides you with more elegant error handling and gives you functions that act as wrappers and catch errors in your own functions.

Install

As with any package, install using your favorite package manager:

pnpm i tryresult

Usage

The base functionality of TryResult revolves around the Result type, which can either be an Ok or an Err. You can create these results using the ok and err functions.

import { ok, err, Result, isOk, isErr } from "tryresult";

// You can create a Result with a value or an error

const success = ok("Success!"); // Result<string, Error>
const failure = err(new Error("Something went wrong")); // Result<string, Error>

// You can do typesafe checks of a Result

if (isOk(success)) {
  console.log(success.value); // "Success!"
}
if (isErr(failure)) {
  console.error(failure.error); // Error: Something went wrong
}

Wrapping Functions

To do away with try-catch blocks around functions you can't control, you can use wrapping functions to automatically get a Result type.

import { tryFn, isErr } from "tryresult";

// Capture throws from async or sync functions

const result = tryFn(() => {
  return JSON.parse('{"message": "Hello!"}');
});

const asyncResult = await tryFn(async () => {
  const response = await fetch('https://api.example.com/data');
  return response.json();
});

// As shown above, the `Result` can be checked

if (isErr(result)) {
  console.error("An error occurred:", result.error); // Guaranteed to be an Error object
} else {
  console.log(result.value.message); // "Hello!"
}

Working with Results

Several utilities are provided to make working with Result types easier, and abstract common functionality.

import { match, okOr, okOrThrow, mapOk, mapErr } from "tryresult";

// Match on the Result type to run different logic or return different values based on whether it's Ok or Err

const greeting = match(result, {
  ok: (value) => `Success: ${value.message}`,
  err: (error) => `Error: ${error.message}`,
});

// Ignore errors and use a default value for when you don't care about the error

const data = okOr(result, { message: "Default message" });

// If need be, you can throw an error if the Result is Err

try {
  const value = okOrThrow(result);
  console.log(value.message);
} catch (error) {
  console.error("Error was thrown:", error);
}

// Returned values can be transformed/mapped quickly

const uppercased = mapOk(result, (value) => value.toUpperCase());

// The same is true for errors, and several mapping functions are provided

const betterError = mapErr(result, (error) => `Custom error: ${error.message}`);

More information can be found as part of function documentation, which can be viewed in your editor or in the source code.