Protects a function from throwing errors by returning a Result object.
Result
success
data
error
const okFn = () => 'ok';const errorFn = () => { throw new Error('error') };const okResult = protect(okFn)(); // { success: true, data: 'ok' }const errorResult = protect(errorFn)(); // { success: false, error: Error('error') } Copy
const okFn = () => 'ok';const errorFn = () => { throw new Error('error') };const okResult = protect(okFn)(); // { success: true, data: 'ok' }const errorResult = protect(errorFn)(); // { success: false, error: Error('error') }
Rest
Protects a function from throwing errors by returning a
Resultobject.successproperty indicates whether the function succeeded or not.dataproperty contains the return value of the function if it succeeded.errorproperty contains the error thrown by the function if it failed.dataanderrorproperties will contain the resolved value and the rejected error respectively.Example