Allows recovering from Error
values with a handler function.
This API is still experimental, so it may change or be removed in future versions. You should not rely on it for production applications.
forall a, b, c:
(Result a b).((a) => Result c b) => Result c b
Allows recovering from Error
values with a handler function.
While .chain()
allows one to sequence operations, such that the second
operation is ran only if the first one succeeds, the .orElse()
method allows
one to recover from an Error
by running a function that provides a new Result
value.
const Result = require('folktale/result');
Result.Ok(4).orElse(error => Result.Ok(error + 1));
// ==> Result.Ok(4)
Result.Error(4).orElse(error => Result.Ok(error + 1));
// ==> Result.Ok(5)
Result.Error(4).orElse(error => Result.Error(error - 1));
// ==> Result.Error(3)
{
/*~*/
Error: function orElse(handler) {
assertFunction('Result.Error#orElse', handler);
return handler(this.value);
},
/*~*/
Ok: function orElse(handler) {
assertFunction('Result.Ok#orElse', handler);
return this;
}
}