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 🙏

© 2024 – Pkg Stats / Ryan Hefner

goerrify

v1.0.2

Published

goerrify is a lightweight utility library that brings Go-style error handling to your JavaScript and TypeScript projects. It provides two simple yet powerful functions, errify and errifyAll, that enable you to handle asynchronous operations with the clari

Downloads

5

Readme

goerrify NPM Version - Bringing Go-style Error Handling to JavaScript

The goerrify package provides utilities to simplify error handling in JavaScript, inspired by Go's approach. It offers two functions:

  • errify: Asynchronously wraps a promise and returns an array containing the resolved value (if successful) or null and the caught error (if rejected).
  • errifyAll: Asynchronously handles a collection of promises using Promise.allSettled, returning an array of arrays where each inner array contains the resolved value or null and the corresponding error (if any).

Installation

npm install goerrify

Usage

CommonJS

const { errify, errifyAll } = require('goerrify');

async function fetchData() {
  try {
    const response = await fetch('https://api.example.com/data');
    return await response.json();
  } catch (err) {
    return null;
  }
}

async function main() {
  const [data, err] = await errify(fetchData());
  if (err) {
    console.error('Error fetching data:', err);
    return;
  }

  console.log('Fetched data:', data);

  // Using errifyAll for multiple promises
  const promises = [fetchData(), anotherPromise(), ...];
  const results = await errifyAll(promises);

  results.forEach(([result, error]) => {
    if (error) {
      console.error('Error in promise:', error);
    } else {
      console.log('Result:', result);
    }
  });
}

main();

Modules (ES6+)

import { errify, errifyAll } from 'goerrify';

async function fetchData() {
  try {
    const response = await fetch('https://api.example.com/data');
    return await response.json();
  } catch (err) {
    return null;
  }
}

async function main() {
  const [data, err] = await errify(fetchData());
  if (err) {
    console.error('Error fetching data:', err);
    return;
  }

  console.log('Fetched data:', data);

  // Using errifyAll for multiple promises
  const promises = [fetchData(), anotherPromise(), ...];
  const results = await errifyAll(promises);

  results.forEach(([result, error]) => {
    if (error) {
      console.error('Error in promise:', error);
    } else {
      console.log('Result:', result);
    }
  });
}

main();

Key Points

  • errify provides a concise way to handle individual promises, returning both the result and any encountered error in a single array.
  • errifyAll is useful for managing collections of promises, allowing you to efficiently process their outputs and errors in a single loop.

Benefits of goerrify

  • Improves code readability and maintainability by promoting a consistent error handling pattern.
  • Simplifies promise handling by avoiding nested try...catch blocks.
  • Provides a familiar approach for developers coming from Go or similar languages.

Additional Considerations

  • While goerrify simplifies common error handling patterns, complex scenarios might still require more elaborate error management strategies.
  • Consider using a library like bluebird for advanced promise handling features.

License MIT GitHub License