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

try2catch

v2.0.3

Published

A better try/catch like way to get your errors.

Readme

Try2Catch

A better try/catch like way to get your errors encapsulated.

Installation

You can install Try2Catch using npm or yarn:

npm install try2catch
yarn add try2catch

Usage

To use Try2Catch, import the Try object from the package:

import { Try } from 'try2catch';

Create a Promise that resolves or rejects after some time, for example:

const awaiter = new Promise((res, reject) => {
    const random = (Math.random() * 10) + 1
    setTimeout(() => { 
            if (random > 5) {
                return res('Nice!');
            }
        
            return reject(new Error('Failed!'));
        }, 5000);
});

An asynchronous function that uses Try to handle the Promise's result:

const processing = async () => {
    const [data, error] = await Try.promise(awaiter)

    if (error) {
        return error;
    }

    return data
}

In this example, Try.promise() takes a Promise as an argument and returns a positional tuple [data, error]. If the Promise resolves successfully, position 0 holds the resolved value and position 1 is null. If the Promise is rejected, position 0 is null and position 1 holds the rejection reason. Exactly one of the two positions is non-null on every settlement.

Settling many Promises

Try.settled() runs an array of Promises in parallel and partitions the outcomes without ever rejecting, returning a [values, reasons] tuple:

const [values, reasons] = await Try.settled([fetchA(), fetchB(), fetchC()]);

values collects every resolved settlement and reasons collects every rejection, each as an [index, value] (or [index, reason]) pair. The index is the original position of the Promise in the input array, so an outcome can always be traced back to the call that produced it:

const [values, reasons] = await Try.settled([fetchA(), fetchB(), fetchC()]);

for (const [index, value] of values) {
  console.log(`Promise ${index} resolved with`, value);
}

for (const [index, reason] of reasons) {
  console.error(`Promise ${index} rejected with`, reason);
}

Inspecting types

Try.typeOf() returns the runtime type tag of any value:

Try.typeOf([]);        // 'Array'
Try.typeOf(null);      // 'Null'
Try.typeOf(new Map()); // 'Map'

Migrating from 1.x to 2.0

Breaking change: Try.promise() no longer returns a wrapper object. It now returns a positional tuple [data, error]. The instance helpers isError(), data, and error were removed, along with the internal TryThen / TryCatch classes (which were never exported).

What changed

  • Return shape: Try.promise() returned a TryThen / TryCatch object in 1.x; in 2.0 it returns a [data, error] tuple.
  • Discrimination: result.isError() is gone; check the second position instead with if (error).
  • Success value: result.data becomes the first position of the tuple.
  • Failure value: result.error becomes the second position of the tuple.
  • New helper: 2.0 adds Try.settled() for handling arrays of Promises in parallel.

How to migrate

Replace the wrapper-object access with tuple destructuring.

Before (1.x):

const result = await Try.promise(awaiter);

if (result.isError()) {
    return result.error;
}

return result.data;

After (2.0):

const [data, error] = await Try.promise(awaiter);

if (error) {
    return error;
}

return data;

Migration tips

  • Find every call site. Search your codebase for Try.promise and for the .isError(), .data, and .error accessors that follow it.
  • Pick your variable names per call. The tuple is positional, not named, so rename freely: const [user, userError] = await Try.promise(...).
  • Mind TypeScript narrowing when destructuring. Checking error does not narrow data to non-null, because they become independent variables. When you need data typed as non-null after an early return, assert it (data!) or guard with if (!error).
  • Drop any imports of TryThen / TryCatch. They were internal and are gone in 2.0; only Try is part of the public API.

License

This package is licensed under the MIT license. See the LICENSE file for more details.