JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 450
  • Score
    100M100P100Q95189F
  • License ISC

Return an Either object instead of throwing an exception

Package Exports

  • might-fail
  • might-fail/dist/index.js

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 (might-fail) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

Might Fail

A TypeScript library for handling async errors without try and catch blocks. Inspired by other languages that utilize Result or Either types for safer error handling.

Quick Start

Install

npm install might-fail

Wrap Promise in mightFail

const {error, result: posts} = await mightFail(fetch("/posts"))

if (error) {
  // handle error
  return 
}
posts!.map(post => console.log(post.title))

Or Wrap Async Function in makeMightFail

const mfFetch = makeMightFail(fetch)
const {error, result: posts} = await mfFetch("/posts")

if (error) {
  // handle error
  return 
}
posts!.map(post => console.log(post.title))

Either Type

awaiting the mightFail functions will return an Either type with either an error or a result.

  • error always has the type Error | undefined.
  • result always has the type T | undefined where T is the type of the result of the promise passed to mightFail.

This means that the you never lose the type information of the result of the promise passed to mightFail.