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.
forall a, b, c:
(Result a b).((b) => Result a c) => Result a c
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.
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')
{
/*~*/
Error: function chain(f) {
assertFunction('Result.Error#chain', f);
return this;
},
/*~*/
Ok: function chain(f) {
assertFunction('Result.Ok#chain', f);
return f(this.value);
}
}