action-debian-package/node_modules/ramda/es/tryCatch.js
Dawid Dziurla 9308795b8b
update
2020-03-26 15:37:35 +01:00

40 lines
1.6 KiB
JavaScript

import _arity from "./internal/_arity.js";
import _concat from "./internal/_concat.js";
import _curry2 from "./internal/_curry2.js";
/**
* `tryCatch` takes two functions, a `tryer` and a `catcher`. The returned
* function evaluates the `tryer`; if it does not throw, it simply returns the
* result. If the `tryer` *does* throw, the returned function evaluates the
* `catcher` function and returns its result. Note that for effective
* composition with this function, both the `tryer` and `catcher` functions
* must return the same type of results.
*
* @func
* @memberOf R
* @since v0.20.0
* @category Function
* @sig (...x -> a) -> ((e, ...x) -> a) -> (...x -> a)
* @param {Function} tryer The function that may throw.
* @param {Function} catcher The function that will be evaluated if `tryer` throws.
* @return {Function} A new function that will catch exceptions and send then to the catcher.
* @example
*
* R.tryCatch(R.prop('x'), R.F)({x: true}); //=> true
* R.tryCatch(() => { throw 'foo'}, R.always('catched'))('bar') // => 'catched'
* R.tryCatch(R.times(R.identity), R.always([]))('s') // => []
* R.tryCatch(() => { throw 'this is not a valid value'}, (err, value)=>({error : err, value }))('bar') // => {'error': 'this is not a valid value', 'value': 'bar'}
*/
var tryCatch =
/*#__PURE__*/
_curry2(function _tryCatch(tryer, catcher) {
return _arity(tryer.length, function () {
try {
return tryer.apply(this, arguments);
} catch (e) {
return catcher.apply(this, _concat([e], arguments));
}
});
});
export default tryCatch;