Giter Club home page Giter Club logo

go-try's Introduction

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());

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.