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

@dschz/try-catch

v1.2.2

Published

Simple try-catch utility function for JavaScript

Readme

@dschz/try-catch

License npm Bundle Size JSR CI

A tiny utility to wrap functions or promises and return a [error, data] tuple — no more try/catch boilerplate.

Features

  • Supports synchronous functions, async functions, and direct promises
  • Returns the appropriate type based on input — no unnecessary await for sync functions
  • Strongly typed with exported Result<T, E>, Success<T>, and Failure<E> types
  • Non-Error throws are automatically wrapped in an Error with cause
  • Zero dependencies

Installation

npm install @dschz/try-catch
pnpm add @dschz/try-catch
yarn add @dschz/try-catch
bun add @dschz/try-catch

Usage

Synchronous functions

Returns Result<T, E> directly:

import { tryCatch } from "@dschz/try-catch";

// Parse JSON
const [err, data] = tryCatch(() => JSON.parse('{"a":1}'));

// Functions that throw
const [err, data] = tryCatch(() => {
  throw new RangeError("Out of bounds");
});

// Non-Error throws are wrapped in Error with cause
const [err, data] = tryCatch(() => {
  throw "string error";
});
// err.message === "string error", err.cause === "string error"

Async functions

Returns Promise<Result<T, E>>:

const [err, data] = await tryCatch(async () => {
  const res = await fetch("/api");
  return res.json();
});

Direct promises

Returns Promise<Result<T, E>>:

const [err, data] = await tryCatch(fetch("/api/data"));
const [err, data] = await tryCatch(Promise.resolve(42));
const [err, data] = await tryCatch(Promise.reject(new Error("fail")));

Promise chains

Returns Promise<Result<T, E>>:

const [err, data] = await tryCatch(() => fetch("/api").then((r) => r.json()));

Important Note

Always wrap expressions that might throw in a function. This ensures the error is caught inside the try-catch scope:

// CORRECT
tryCatch(() => JSON.parse("{ malformed }"));

// INCORRECT — JSON.parse evaluates and throws before tryCatch is even called
tryCatch(JSON.parse("{ malformed }"));

Types

All types are exported for use in your own type definitions:

import { tryCatch, Result, Success, Failure } from "@dschz/try-catch";

type Success<T> = [error: null, data: T];
type Failure<E extends Error = Error> = [error: E, data: null];
type Result<T, E extends Error = Error> = Success<T> | Failure<E>;

The return value is a tuple where one value will always be null:

const [error, data] = tryCatch(() => someOperation());

if (error) {
  // handle error
  return;
}

// data is available here

Custom Error Types

class MyError extends Error {
  constructor(message: string) {
    super(message);
    this.name = "MyError";
  }
}

const [err, data] = await tryCatch<MyType, MyError>(async () => doSomething());

License

MIT © Daniel Sanchez