Encapsulates a common pattern of needing to build up an Object from
a series of Result values. This is often solved by nesting andThen
calls
and then completing the chain with a call to ok
.
This feature was inspired (and the code lifted from) this article: https://medium.com/@dhruvrajvanshi/simulating-haskells-do-notation-in-typescript-e48a9501751c
Wrapped values are converted to an Object using the Object constructor before assigning. Primitives won't fail at runtime, but results may be unexpected.
Folds over types; a switch/case for success or failure.
Inject a side-effectual operation into a chain of Result computations.
The primary use case for do
is to perform logging in the middle of a flow
of Results.
The side effect only runs when there isn't an error (Ok).
The value will (should) remain unchanged during the do
operation.
ok({}) .assign('foo', ok(42)) .assign('bar', ok('hello')) .do(scope => console.log('Scope: ', JSON.stringify(scope))) .map(doSomethingElse)
Returns the value from a successful result. For an error, returns the result of evaluating the fn
Returns the value from a successful result. Returns the defaultValue if the result was a failure.
Returns a new result after applying fn to the value stored in a successful result. If the result was a failure, then the Err result is simply returned.
Returns a new result after applying fn to the error value. successful results are returned unchanged.
Generated using TypeDoc
A Result represents a computation that may succeed or fail. Ok represents
a successful computation, while Err represents a failure.