refactor: make console log less verbose when not needed

This commit is contained in:
鲁树人
2023-05-22 00:00:35 +01:00
parent 63fff9be3a
commit bb633cd6f2
6 changed files with 52 additions and 23 deletions

20
src/util/fnWrapper.ts Normal file
View 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;
}
}