function isPromise(p: unknown): p is Promise { return !!p && typeof p === 'object' && 'then' in p && 'catch' in p && 'finally' in p; } export function wrapFunctionCall(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; } }