mirror of
https://github.com/swc-project/swc.git
synced 2024-12-22 13:11:31 +03:00
42 lines
1.6 KiB
TypeScript
42 lines
1.6 KiB
TypeScript
|
// Loaded from https://deno.land/x/ramda@v0.27.2/source/tryCatch.js
|
||
|
|
||
|
|
||
|
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 them to the catcher.
|
||
|
* @example
|
||
|
*
|
||
|
* R.tryCatch(R.prop('x'), R.F)({x: true}); //=> true
|
||
|
* R.tryCatch(() => { throw 'foo'}, R.always('caught'))('bar') // =>
|
||
|
* 'caught'
|
||
|
* 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 = _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;
|