You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

18 lines
419 B

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
}
}
}