blob: 0902e65a762195561a4b76611565a8079cbf8b8b [file] [log] [blame] [edit]
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
}
}
}