_try

Runs a function that may raise an exception, trapping it. Returns an Ok with the return value of the function, if it has finished successfully, or an Error with the raised exception.

This feature is experimental!

This API is still experimental, so it may change or be removed in future versions. You should not rely on it for production applications.

Signature

_try(f)
forall a, b: (() => b :: throws a) => Result a b

Documentation

Runs a function that may raise an exception, trapping it. Returns an Ok with the return value of the function, if it has finished successfully, or an Error with the raised exception.

Example:

function successor(natural) {
  if (natural < 0) {
    throw `Not a natural number: ${natural}`;
  } else {
    return natural + 1;
  }
}

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

Result.try(() => successor(-1));
// ==> Result.Error('Not a natural number: -1')

Result.try(() => successor(1));
// ==> Result.Ok(2)

Properties

Source Code

Defined in source/result/try.js at line 20, column 0
(f) => {
  try {
    return Ok(f());
  } catch (e) {
    return Error(e);
  }
}
Stability
experimental
Licence
MIT
Module
folktale/result/try
Authors
Copyright
(c) 2013-2017 Quildreen Motta, and CONTRIBUTORS
Authors
  • @boris-marinov
Maintainers
  • Quildreen Motta <queen@robotlolita.me> (http://robotlolita.me/)