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

go-try

v3.0.2

Published

Easy and flattened error handling for sync and async functions

Downloads

1,813

Readme

go-try

Build status License Version

go-try is a zero dependency npm package inspired by Go programming language (Golang). Its main goal is to reduce use of try-catch blocks in JavaScript/TypeScript code and make exception handling easier with a more flattened structure. It can be used with async or sync functions. It's compatible with both browser and server-side applications.

Async Example

Let's say that we want to fetch some user and product data from an outer source (like a database, or an API etc) and create an order. To do that, we need to run an some async functions and continue if they succeed. And we need to handle the exceptions, if there are any.
With try-cath blocks, you can do it like this:

async function main() {
  try {
    const userId = 123;
    const user = await getUser(userId);
    try {
      const productId = 456;
      const product = await getProduct(productId);
      try {
        const order = await createOrder(user, product);
        return order;
      } catch (err) {
        console.log('Create order failed');
      }
    } catch (err) {
      console.log('Fetch product failed');
    }
  } catch (err) {
    console.log('Fetch user failed');
  }
}

It's handy, but it can create some hard to read, pyramid style code. Sometimes flat code is easier to read and more clear.
So, the code turns into this with go-try:

import { goTry } from 'go-try';

async function main() {
  const userId = 123;
  const [userError, user] = await goTry(() => getUser(userId));
  if (userError) {
    console.log('Get user failed');
    return;
  }

  const productId = 456;
  const [productError, product] = await goTry(() => getProduct(productId));
  if (productError) {
    console.log('Get product failed');
    return;
  }

  const [orderError, order] = await goTry(() => createOrder(user, product));
  if (orderError) {
    console.log('Create order failed');
    return;
  }
  return order;
}

go function returns a promise that resolves with an array. The array has 2 items.
First item is the exception that your async function throws. If the function succeeds, it will be null.
Second item is the response of your async function. If the function fails, it will be null. Otherwise, it's whatever the async function returns.
So basically, if error is null, your async function didn't throw an error. All we need to do is to check it before running rest of our code.

Sync Example

Everything above applies for sync functions too. The only difference is, you need to use goTrySync instead of goTry.

import { goTrySync } from 'go-try';

const [error, data] = goTrySync(() => someSyncFunction());
if (error) {
  console.log('Failed');
}
// Rest of the code

Destructuring with TypeScript

If you are using a typescript version below v4.6.0, you will come accross a ts error like below:

const [error, data] = await go(() => getUser());
if (error) {
  console.log('Failed');
  return;
}

// At this point, TypeScript (if the version is below `4.6.0`) can't understand because the `error` is `null`,
// `data` should be the type of what's resolved by promise returned by `getUser` function.
// TypeScript still thinks `data` can be `null`.
// We need an extra check like:
if (!data) {
  return;
}

You can check here for more information.

With TypeScript

Basically, you don't need to specify type of your returned data. goTry and goTrySync are generic functions. So, they can understand what is your data type by themselves. But if you want to explicitly specify your data and error type, you can do that too.
For example:

// Let's say that your function returns an array of some users.
// And it throws a custom error instance of a class that extends Error
// (like a NotFoundError, AxiosError etc)
interface User {
  id: string;
  firstName: string;
  lastName: string;
}

// ...
const [error, users] = await goTry<User[], NotFoundError>(() => getUsers());
// or
const [error, users] = goTrySync<User[], NotFoundError>(() => getUsersSync());

// Or you can just specify your data type if your error is a classic Error instance
const [error, users] = await goTry<User[]>(() => getUsers());
// or
const [error, users] = goTrySync<User[]>(() => getUsersSync());