mirror of
https://git.um-react.app/um/um-react.git
synced 2025-11-28 19:43:02 +00:00
refactor: make console log less verbose when not needed
This commit is contained in:
20
src/util/fnWrapper.ts
Normal file
20
src/util/fnWrapper.ts
Normal file
@@ -0,0 +1,20 @@
|
||||
function isPromise<T = unknown>(p: unknown): p is Promise<T> {
|
||||
return !!p && typeof p === 'object' && 'then' in p && 'catch' in p && 'finally' in p;
|
||||
}
|
||||
|
||||
export function wrapFunctionCall<R = unknown>(pre: () => void, post: () => void, fn: () => R): R {
|
||||
pre();
|
||||
|
||||
try {
|
||||
const result = fn();
|
||||
|
||||
if (isPromise(result)) {
|
||||
result.finally(post);
|
||||
}
|
||||
|
||||
return result;
|
||||
} catch (e) {
|
||||
post();
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user