Skip to content
Maciej Sitko edited this page Mar 30, 2017 · 51 revisions

Result is an abstraction layer and the container type similar to that of Result in Rust programming language or Either in Haskell.


Abstract parent class for Err and Ok subclasses, indicates any result value.

.unwrap()

Returns the value currently held as Result's x.

Example:

const result = Ok.of(1234);
// Also possible to use new constructor, as in `new Ok(1234)`.
// It is, however, considered safer to use `.of` in case you'd miss
// constructor.

result.unwrap(); // 1234 

.unwrapOr(x)

Execute unwrap. If type is Ok, then returns a value, if not, returns an alternative provided value x.

.unwrapOrElse(fn)

Execute unwrap.Iif Ok, then returns a value, if not, executes a callback function fnand then returns the resulting value.

.isOk()

Determines whether a result is Ok type of container.

Example:

result.isOk() // true

.isErr()

Determines whether a result is Err type of container.

Example:

result.isErr() // false

.inspect()

Returns a string with representation of a container type and current x value. Helpful for pattern matching.

Example:

switch(result.inspect()) {
  case 'Ok([object Promise])':
    parseAndDo(result.unwrap());
    break;
  case 'Err(404)':
    catch404(result.unwrap());
    break;
  default: 
    otherwise(result.unwrap())
    break;
}

Also known as Either.Left, indicates abstraction layer on top of more low level error handling, through catch(err).

.map(_)

Returns a constant, would not map the value if Err.

Example:

const error = new Err('Error has occurred.');
// Also possible to use new constructor, as in `new Err('Error has occurred.')`.
// It is, however, considered safer to use `.of` in case you'd miss
// constructor.

const mapped = error.map(x => true);
mapped.unwrap(); // still 'Error has occurred.'

.orElse(fn)

Will map the Err container value. Won't map if type is Ok.

const error = Err.of('Error has occurred.');

const mapped = error.map(x => true)
     .orElse(_ => 'Hey! I am new here.');
mapped.unwrap(); // now 'Hey! I am new here.'

.andThen(fn)

It will call fn on the promise value within Result container. The next returned value will be a new promise. If not, and check for Promise presence fails, it will fall back to a .map default behavior.

Note: There is a clear and intentional distinction: calling .andThen is NOT the same as calling .then, but rather a simple wrapper around it for executing some side effects.

client.request(API_GET['200'])
   .then(Result => Result.andThen(handleOk).orElse(x => 'Error'))
                         .andThen(x => {success: x})); 
// ^ above will always ignore `.orElse` if successful.

Or Either.Right, indicates similar alternative for successful try attempt.

.map(fn)

will map the Ok container value.

Example:

const ok = {
 value: Ok.of(1234),
 deferred: Ok.of(new Promise(res => res(1234)))
}

const value = ok.value.map(x => x * 2);
value.unwrap(); // now 2468

const deferred = ok.deferred.map(x => x.then(v => v * 2));
deferred.unwrap().then(x => x); // now 2468

Note: The above Promise example is crude in its nature as .map is a very low-level way of resolving any asynchronous result within the container. For a better way, see .andThen.

.orElse(_)

returns a constant, would not map the value.

Example:

const ok = new Ok('Some value.');

const mapped = ok.orElse(_ => 'Some other value!');
mapped.unwrap(); // now 'Some value.'