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

goerr

v2.0.0

Published

handle errors in a better way.

Downloads

172

Readme

goerr

Handle errors in a better way.

Install

# install with npm
npm add goerr
# install with yarn
yarn add goerr
# install with pnpm
pnpm add goerr

Import

// ES Module
import goerr from 'goerr'
// CommonJS
const goerr = require('goerr').default

Usage

Handle A Synchronous Function Calling

goerr method expects a function (especially a lambda expression) as the argument, in which calling some throwable functions and returning a value.

The value will be the first element of an array returned by goerr. Any error throwed when calling the function will be the second element of the array.

Note: You cannot return a Promise object in the throwableFunction. If you want to handle asynchronous errors, see the next section.

API

function goerr<T>(func: () => T): [T, Error]

Example

const [result, err] = goerr(() => throwableFunction())

if (err) {
  // do something here
}

// go ahead with the result
console.log(result)

Handle An Asynchronous Promise

goerr can accept a Promise object in order to handle cases you have to do asynchronously.

API

function goerr<T>(promise: Promise<T>): Promise<[T, Error]>

Example

const [result, err] = await goerr(axios.get('https://example.com'))

if (err) {
  // do something here
}

// go ahead with the result
console.log(result)

Handle An Asynchronous Function Calling

goerr can accept an asynchronous function in which you could use async/await.

API

function goerr<T>(asyncFunc: () => Promise<T>): Promise<[T, Error]>

Example

const [result, err] = await goerr(async () => {
  const num1 = await calculateAsync()
  const num2 = await calculateAsync()
  return num1 + num2
})

if (err) {
  // do something here
}

// go ahead with the result
console.log(result)

Why This

Background

Since we all know that try/catch works good in JavaScript, you might be confused about if we really need goerr to handle errors.

Suppose now we are writing a function to return a list coming from a throwable function, and return an empty list when an error throwed out.

How could we do?

Solution 1

function listOf() {
  try {
    const list = throwable()
    return list
  } catch (_) {
    return []
  }
}

It looks good, but what if we need to do some operations on this list?

Solution 2

function listOf() {
  try {
    const list = throwable()

    for (let item of list) {
      // do something with items
    }

    return list
  } catch (_) {
    return []
  }
}

Good job! But if we have a better choise?

Solution 3

The defect of solution 2 is about code smell. We should return as soon as we can to avoid nested code.

function listOf() {
  const [list, err] = goerr(throwable)

  if (err) {
    return []
  }

  for (let item of list) {
    // do something with items
  }

  return list
}

Now you see the for loop comes to the top level of the function. It looks more clear, right?