Files
pixelheros/extensions/Excel转JSON/node_modules/atomically/src/utils/attemptify.ts
walkpan 8f57f57c1d dd
2024-08-18 23:38:55 +08:00

43 lines
718 B
TypeScript

/* IMPORT */
import {NOOP} from '../consts';
import {Exception, FN} from '../types';
/* ATTEMPTIFY */
//TODO: Maybe publish this as a standalone package
//FIXME: The type castings here aren't exactly correct
const attemptifyAsync = <T extends FN> ( fn: T, onError: FN<[Exception]> = NOOP ): T => {
return function () {
return fn.apply ( undefined, arguments ).catch ( onError );
} as T;
};
const attemptifySync = <T extends FN> ( fn: T, onError: FN<[Exception]> = NOOP ): T => {
return function () {
try {
return fn.apply ( undefined, arguments );
} catch ( error ) {
return onError ( error );
}
} as T;
};
/* EXPORT */
export {attemptifyAsync, attemptifySync};