object-result
Result object represent either an error or a successful computation.
Usage
Results are never created directly, instead object-assign exposes two factory functions, createErr and createOk, that construct result objects representing an error and an ok respectively.
let objectResult = ok = objectResultok err = objectResulterr; // creates a success result with an object used for the success value let goodKitty = ; // creates an error result with an object used for the error value let badKitty = ;
Sometimes you want to do some transformation on a success or error -- but you dont want to modify the relative success-state of the result. In this case, you would use the map/map_err methods.
// map the goodKitty's success value from the object (from above) to just // a string. goodKitty; // map the badKitty's error value from the object above to just a string badKitty; // map only effects ok's and map_err only effects err's // so... goodKitty; badKitty; // have no effect
Sometimes more flexibility is needed -- in particular you may need continue a computation but only if no error has occured. In this case, one should use the and_then/or_else methods.
// and_then is good for continuing successful computations let newKitty = goodKitty; // or_else is good for handling errors -- or transforming them into successes let newKitty = badKitty;
It can be useful to chain or_else or and_then methods together. However, as a word of caution -- generally it is a mistake to chain or_else and and_then methods simultaneously. Use multiple and_then's to continue a successful computation, or use multiple or_else's to do complex error management, but don't try to do both at the same time! (it can get confusing very quickly)
Usually when you want to chain and_then with an or_else you should really be using the match method. Match is effectively and_then and or_else together, but because it isn't chained it is less suprising.
let someKitty = goodKitty;