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

good-result

v1.2.1

Published

The `result` function helps you execute a function or promise while capturing errors in a structured way. It ensures that results and errors are explicitly handled using a `Result<E, T>` type.

Readme

result Utility Function

The result function helps you execute a function or promise while capturing errors in a structured way. It ensures that results and errors are explicitly handled using a Result<E, T> type.

Installation

If this is part of an npm package, install it as:

npm install good-result

Usage

Basic Example

const goodFn = async () => 'Success!'
const badFn = async () => {
  throw new Error('Something went wrong')
}

const goodValue = await result(goodFn)
if (goodValue.err) return response(500, "Internal Server Error")

doSomething(goodValue.val)

const badValue = await result(badFn)
if (badValue.err) return response(500, "Internal Server Error")

doSomething(badValue.val) // doesn't get here

Return Type: Result<E, T>

The function returns an object of type:

type Result<E, T> =
  | { err: NonNullable<E>; val: null }  // When an error occurs
  | { err: null; val: NonNullable<T> }; // When successful

This means you should always check result.err before using the result.val

ESLint Rule

To ensure proper usage, you can use the eslint-plugin-good-result ESLint rule. This rule enforces that any result from result is followed by an if (result.err) check.

ESLint Rule Installation

npm install --save-dev eslint-plugin-good-result

Configuration

Add it to your ESLint configuration:

{
  "plugins": ["good-result"],
  "rules": {
    "good-result/ensure-error-check": "error"
  }
}

This will enforce:

const value = await result(fetchSomething);
if (value.err) {
  console.error(value.err);
  return;
}

console.log(value.val); // Safe to access

Options

The function accepts an optional configuration object:

1. Custom Error Handler

You can transform errors before they are returned.

const resultValue = await result(someFunction, {
  errorHandler: (error) => new CustomError(`Wrapped: ${error}`)
});

2. Handling null as an Error

If your function returns null, you can treat it as an error by providing NoResultError.

const fetchData = async () => null;

const resultValue = await result(fetchData, {
  noResultError: new NoResultError('No data found', 404),
});

if (resultValue.err) {
  console.error(resultValue.err.message); // Logs: No data found
}

When to Use result

  • ✅ Wrapping async functions to prevent uncaught errors
  • ✅ Standardizing error handling in APIs or services
  • ✅ Avoiding try/catch blocks everywhere
  • ✅ Distinguishing between errors and expected null results

FAQ

❓ What happens if I pass a synchronous function?

result works for both synchronous and asynchronous functions:

const syncFunction = () => 'Hello, World!';
const resultValue = await result(syncFunction);
console.log(resultValue.val); // "Hello, World!"

❓ What if my function throws a non-Error value?

If an error is not an instance of Error, it is wrapped in a generic Error object.

Conclusion

The result utility function ensures structured error handling while allowing you to control how null values are treated. It simplifies async error management, making your code cleaner and safer.