Files
classroomio/apps/dashboard/src/lib/utils/functions/trycatch.ts
T

15 lines
296 B
TypeScript
Raw Normal View History

2024-10-06 01:20:32 +01:00
export function tc<T>(tryFunc: () => T, catchFunc: T | ((e?: Error) => T)) {
let val: T;
try {
val = tryFunc();
} catch (e: unknown) {
if (typeof catchFunc === 'function') {
// @ts-ignore
val = catchFunc(e);
} else {
val = catchFunc;
}
}
return val;
}