orElse

Allows recovering from Error values with a handler function.

Signature

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

Documentation

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.

Example:

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)

Properties

Source Code

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

    /*~*/
    Ok: function orElse(handler) {
      assertFunction('Result.Ok#orElse', handler);
      return this;
    }
  }
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/)