rambdax
Version:
Extended version of Rambda - a lightweight, faster alternative to Ramda
19 lines (15 loc) • 419 B
JavaScript
import { type } from './type.js'
const isFunction = x => [ 'Promise', 'Function' ].includes(type(x))
export function tryCatch(fn, fallback){
if (!isFunction(fn)){
throw new Error(`R.tryCatch | fn '${ fn }'`)
}
const passFallback = isFunction(fallback)
return (...inputs) => {
try {
return fn(...inputs)
} catch (e){
return passFallback ? fallback(e, ...inputs) : fallback
}
}
}