A minimal, Flow-typed, Rust-style Result library for functional exception handling.
npm install minimal-resultA minimal, Flow-typed, Rust-style Result library for functional exception handling.
A result type is a disjoint union with two type variables. A result value contains _either_ a data or error value.
``js`
type Result =
{| tag: 'Ok', +data: Data |}
| {| tag: 'Err', +err: Error |}
A function should return a Result when errors are expected and recoverable.
Consider a parsing function that accepts a string and returns an abstract syntax tree ((s: string) => AST). If the string cannot be parsed - if it is malformed - the function should return an error indicating which line/character the error occurs on. If it can be parsed, it ought to return an AST. In other words, the function should have the type (s: string) => Result where ParseError might resemble { explanation: string, line: number, character: number }.
For more on the Result types, see the Rust documentation and the r-result rationale section.
r-result is a similar javascript library. The publication of minimal-result was motivated by a desire for a library which:
- Has simple flow types, and is published with them.
- Offers a static function, rather than method-based API.
- OkData
- Purpose: Take a value of type , return an Ok Result with this value.(data: Data) => Result
- Type: Err
- Error
- Purpose: Take a value of some type , return an Err Result with this value.(err: Error) => Result
- Type: andThen
- Result
- Purpose: Given a and a function that returns a Result, apply the function to the value, unless the value is an Err, in which case simply return it unaltered. This can be chained together to provide "fall through" flow control.(result: Result, f: Data => Result
- Type:
- mapOkData
- Purpose: Apply a function to the value within a Result if it is Ok, wrap the result in an Ok. Err falls through.(result: Result, f: Data => NewData) => Result
- Type: mapErr
- (result: Result, f: Error => NewError) => Result
- Type: unwrapOrElse
- (result: Result, f: Error => Data) => Data
- Type: collectResultMap
-
- Type: collectResultArray
-
- Type: collectResultArrayIndexed
-
- Type:
MIT