JSPM

  • Created
  • Published
  • Downloads 2248551
  • Score
    100M100P100Q189980F
  • License MIT

Stop throwing errors, and instead return Results!

Package Exports

  • neverthrow
  • neverthrow/dist

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

Readme

NeverThrow

Description

Encode failure into your program.

This program contains a Result type that represents either success (Ok) or failure (Err).

This package works for both JS and TypeScript. However, the types that this package provides will allow you to get compile-time guarantees around error handling if you are using TypeScript.

neverthrow draws inspiration from Rust, and Elm. It is also a great companion to fp-ts.

Installation

> npm install neverthrow

Usage

Create Ok or Err instances with the ok and err functions.

import { ok, err } from 'neverthrow'

// something awesome happend

const yesss = ok(someAesomeValue)

// moments later ...

const mappedYes = yesss.map(doingSuperUsefulStuff)

more documentation coming soon :) ... the source code + tests are pretty self-explanatory though!

Wrapping a Dependency that throws

incomplete ... Examples to come soon

  • axios
  • knex

A note on the Package Name

Although the package is called neverthrow, please don't take this literally. I am simply encouraging the developer to think a bit more about the ergonomics and usage of whatever software they are writing.

Throwing and catching is very similar to using goto statements - in other words; it makes reasoning about your programs harder. Secondly, by using throw you make the assumption that the caller of your function is implementing catch. This is a known source of errors. Example: One dev throws and another dev uses the function without prior knowledge that the function will throw. Thus, and edge case has been left unhandled and now you have unhappy users, bosses, cats, etc.

With all that said, there are definitely good use cases for throwing in your program. But much less than you might think.