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

@assertchris/attempt-promise

v1.0.0

Published

Alternative to try-catch wrapping async/await

Downloads

90

Readme

async and await are great! SO much better than callbacks all over the place, and helpful for showing that functions are asynchronous, without having to read the function.

What's not so great is having to wrap calls to async functions in a try-catch.

Maybe this code looks like something in your application:

const id = session.get("id");

let user = undefined;
let products = undefined;

try {
  user = await User.find(id);

  try {
    products = await Product.where("user_id", user.id)
      .orderBy("updated_at", "desc")
      .limit(50)
      .fetch();
  } catch (e) {
    response.error("Oops! Missing products...");
    return;
  }
} catch (e) {
  response.error("Oops! No user...");
  return;
}

response.ok("here are your products...", products);

This is ok, but there are a couple things I don't like about it:

  1. I have to pre-define variables, or they're hidden in the scope of the try-catches
  2. The error handling, for the missing user, is far away from the attempt to fetch the user
  3. There are multiple levels of nesting, for what is supposed to be a linear process

I feel like the gains of being able to avoid promise callbacks are lost by having to wrap everything in a try-catch. I was inspired by some syntax from Go:

user, err := User.find(id)

if err != nil {
    log.Fatal(err)
}

// do something with user

So, I implemented something similar, here. Check out how the first example changes, given this new syntax:

const attempt = require("@assertchris/attempt-promise");

const id = session.get("id");

const [err1, user] = await attempt(User.find(id));

if (err1) {
  response.error("Oops! No user...");
  return;
}

const [err2, products] = await attempt(
  Product.where("user_id", user.id)
    .orderBy("updated_at", "desc")
    .limit(50)
    .fetch(),
);

if (err2) {
  response.error("Oops! Missing products...");
  return;
}

response.ok("here are your products...", products);

Use one of these to install:

npm install @assertchris/attempt-promise
yarn add @assertchris/attempt-promise