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

@drunkcod/promise-result

v0.0.9

Published

Promise.result because, yeah.

Readme

@drunkcod/promise-result

Promise.result because, yeah.

A lightweight, zero-dependency utility to bring Go-style/Rust-style error handling to TypeScript. It eliminates try/catch nesting by converting promises and functions into a Result type that is easy to narrow and safe to use.

Features

  • Type-safe Narrowing: Full TypeScript support for Result<T> which distinguishes between success and error states.
  • Prototype Extension: Optional Promise.prototype.result() for ergonomic chaining.
  • Error Normalization: Automatically converts non-error throws (like strings) into proper Error objects with preserved stack traces.
  • Multi-style Access: Use object destructuring ({ error, value }) or array destructuring (.toArray()).

Installation

npm install @drunkcod/promise-result

Usage

Promises (The bread and butter)

By importing the main package, Promise is extended with the .result() method. This allows for clean handling of async operations without try/catch.

import '@drunkcod/promise-result';

const { error, value: user } = await fetchUser(id).result();

if (error) {
  console.error('Failed to fetch user:', error.message);
  return;
}

// 'user' is now narrowed and safe to use
console.log(user.name);

Synchronous Functions

Wrap existing functions to make them safe without manual try/catch.

import { result } from '@drunkcod/promise-result';

const safeJSON = result(JSON.parse);

const { error, value } = safeJSON('{ "malformed": json }');

if (error) {
  // error is a guaranteed Error object
  return;
}

console.log(value);

Multi-style Access

While object destructuring is preferred for type narrowing, you can also use array destructuring via .toArray() if that fits your style.

import { safeCall } from '@drunkcod/promise-result';

const [err, greeting] = safeCall((name: string) => `Hello ${name}`, null, 'World').toArray();

API

Result<T>

The core type returned by all utilities. It is a union of:

  • { error: Error; value: null; }
  • { error: null; value: T; }

Both variants include a .toArray() method that returns [Error | null, T | null].

Promise.prototype.result()

Extends the global Promise interface. Returns a Promise<Result<T>>.

result(fn)

Returns a new function that wraps fn. The new function returns a Result. It preserves this context when called or bound.

ensureError(err, contextFn)

A utility used internally (but exported) that ensures any thrown value is converted to an Error. If the value isn't an Error, it creates a RejectionError and uses Error.captureStackTrace to keep the stack trace clean.

Why use this?

try/catch blocks create extra indentation and often force you to declare variables with let outside the block scope.

Before:

let user;
try {
  user = await fetchUser();
} catch (e) {
  const error = e instanceof Error ? e : new Error(String(e));
  handleError(error);
  return;
}
console.log(user.id);

After:

const { error, value: user } = await fetchUser().result();
if (error) return handleError(error);

console.log(user.id);

License

MIT