0.4.2 • Published 4 years ago

@dise-international/result v0.4.2

Weekly downloads
108
License
ISC
Repository
-
Last release
4 years ago

Result

Result is a data structure heavily inspired by the Rust Result enum that encodes successful results, or errors. Sometimes called Either, it's made up of a tagged union with two variants:

  • Ok (x), representing a successrul result
  • Err (e), representing an error.

Result is useful in place of throw, and offers several benefits:

  • Result offers semantic information to the developer - Result<number, string> tells us both that it is an Result, and that it should contain a number when successful, or an error message otherwise.
  • It forces us to deal with the errors, while providing ergonomic, expressive ways to do so. Exceptions can be ignored, Results can't.
  • No more uncaught exceptions or unhandled rejections.
  • Offers a clear distinction between catastrophic, unforeseen errors, and application errors that we enumerate and control.

Similar to @dise-international/option, the Result instance implements only a few basic methods and properties, while more expressive functions are implemented as static methods. This is to keep the actual class instance clean, and to promote piping values through functional transformations.

Installation

@dise-international/result is available in both TypeScript and JavaScript format.

Install via NPM

$ npm install @dise-international/result

Import the module

import { Result } from '@dise-international/result';

Or require it

const { Result } = require('@dise-international/result');

Usage

0.4.2

4 years ago

0.4.1

4 years ago

0.4.0

4 years ago

0.3.0

4 years ago

0.2.1

4 years ago

0.2.0

4 years ago

0.1.0

4 years ago