@dise-international/result
v0.4.2
Published
A Rust-inspired Result type/fake monad. Use this to handle values that can either be successful, or a failure/error.
Downloads
171
Readme
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 resultErr (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 anResult
, 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');