Skip to content
This repository has been archived by the owner on Oct 13, 2021. It is now read-only.
/ toolbelt Public archive

Useful utility functions for Kimmel projects

License

Notifications You must be signed in to change notification settings

kimmelsg/toolbelt

Repository files navigation

Toolbelt

Useful utility functions for Kimmel projects

codecov

Installation

In your terminal:

$ yarn add @kimmel/toolbelt

Functions

attempt

Wraps a promise in a try/catch block and returns a tuple with the first item being any caught error, and the second item being the resolved promise.

import { attempt } from '@kimmel/toolbelt';

async function fetchData() {
  return fetch('https://example.com');
}

async function tryToFetch() {
  const [error, result] = await attempt(fetchData);

  if (error) {
    // do something
  }

  return result;
}

attempt automatically infers the resolves type of your promise.