chain

Transforms the value and context of a Result computation with an unary function. As with .map(), the transformation is only applied if the value is an Ok, but the transformation is expected a new Result value, which then becomes the result of the method.

Signature

forall a, b, c:
  (Result a b).((b) => Result a c) => Result a c

Documentation

Transforms the value and context of a Result computation with an unary function. As with .map(), the transformation is only applied if the value is an Ok, but the transformation is expected a new Result value, which then becomes the result of the method.

Example:

const Result = require('folktale/result');

const divideBy = (a) => (b) =>
  a === 0        ?  Result.Error('division by zero')
: /* otherwise */   Result.Ok(b / a);


Result.Ok(4).chain(divideBy(2));
// ==> Result.Ok(2)

Result.Error(4).chain(divideBy(2));
// ==> Result.Error(4)

Result.Ok(4).chain(divideBy(0));
// ==> Result.Error('division by zero')

Properties

Source Code

Defined in source/result/result.js at line 75, column 19
{
    /*~*/
    Error: function chain(f) {
      assertFunction('Result.Error#chain', f);
      return this;
    },

    /*~*/
    Ok: function chain(f) {
      assertFunction('Result.Ok#chain', f);
      return f(this.value);
    }
  }
Stability
stable
Licence
MIT
Module
folktale/result/result
Authors
Copyright
(c) 2013-2017 Quildreen Motta, and CONTRIBUTORS
Authors
  • Quildreen Motta
Maintainers
  • Quildreen Motta <queen@robotlolita.me> (http://robotlolita.me/)