Calls the callback and returns its output. If the callback throws an error, it is handled in the following ways:
handleError
options
wrapInTry
fallbackValue
import {wrapInTry} from '@augment-vir/common';// `result1` will be `'success'`.const result1 = wrapInTry( () => { return 'success'; }, { fallbackValue: 'failure', },);// `result2` will be `'failure'`.const result2 = wrapInTry( () => { throw new Error(); return 'success'; }, { fallbackValue: 'failure', },);// `result3` will be `'failure also'`.const result3 = wrapInTry( () => { throw new Error(); return 'success'; }, { handleError() { return 'failure also'; }, },);// `result4` will be `'failure also'`.const result4 = wrapInTry( () => { throw new Error(); return 'success'; }, { handleError() { return 'failure also'; }, fallbackValue: 'ignored', },); Copy
import {wrapInTry} from '@augment-vir/common';// `result1` will be `'success'`.const result1 = wrapInTry( () => { return 'success'; }, { fallbackValue: 'failure', },);// `result2` will be `'failure'`.const result2 = wrapInTry( () => { throw new Error(); return 'success'; }, { fallbackValue: 'failure', },);// `result3` will be `'failure also'`.const result3 = wrapInTry( () => { throw new Error(); return 'success'; }, { handleError() { return 'failure also'; }, },);// `result4` will be `'failure also'`.const result4 = wrapInTry( () => { throw new Error(); return 'success'; }, { handleError() { return 'failure also'; }, fallbackValue: 'ignored', },);
@augment-vir/common
Optional
Calls the callback and returns its output. If the callback throws an error, it is handled in the following ways:
handleError
function is provided inoptions
, it is passed the thrown error. The output ofhandleError
is returned bywrapInTry
.fallbackValue
is provided, it is returned bywrapInTry
. The thrown error is ignored.wrapInTry
.Example
Package
@augment-vir/common