JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 8
  • Score
    100M100P100Q76650F
  • License MIT

A better way to handle errors

Package Exports

  • betterr
  • betterr/dist/index.js
  • betterr/dist/index.mjs

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (betterr) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

betterr

A better way to handle errors

build status coverage

Advantages

  • File structure remains flat, unlike with nested try...catch
  • Both data and errors are declared with const, unlike with non-nested try...catch
  • Both data and errors are non-nullable, once an early return occurs if the other is null
  • Both data and errors are available at the top level, unlike with try..catch or promises
  • Work with errors that are always Error objects, without compromising type-safety, unlike with try...catch or promises

Installation

$ npm install betterr

Usage

import { betterr, betterrSync } from 'betterr';
// const { betterr, betterrSync } = require('betterr');

async function main() {
  const { data: user, err } = await betterr(() => getRandomUser());
  //            ^?    ^? user: User | null, err: Error | null

  if (err) {
    // ^? err: Error
    return;
  }

  return user;
  //     ^? user: User
}
  • betterr can be used with both asynchronous and synchronous callbacks
  • betterrSync can only be used with synchronous callbacks, but avoids wrapping the data in a promise, so await is not necessary

TypeScript

Both betterr and betterrSync are generic, so the type of data can be provided so long as the callback return type is assignable to the generic parameter

const { data, err } = betterrSync<User>(() => ({
  //    ^? data: User
  userId: 1,
}));