mirror of
https://github.com/hay-kot/homebox.git
synced 2024-11-22 08:35:43 +00:00
20 lines
569 B
TypeScript
20 lines
569 B
TypeScript
|
type DeferFunction<TArgs extends any[], TReturn> = (...args: TArgs) => TReturn;
|
||
|
|
||
|
// useDefer is a function that takes a function and returns a function that
|
||
|
// calls the original function and then calls the onComplete function.
|
||
|
export function useDefer<TArgs extends any[], TReturn>(
|
||
|
onComplete: (...args: TArgs) => void,
|
||
|
func: DeferFunction<TArgs, TReturn>
|
||
|
): DeferFunction<TArgs, TReturn> {
|
||
|
return (...args: TArgs) => {
|
||
|
let result: TReturn;
|
||
|
try {
|
||
|
result = func(...args);
|
||
|
} finally {
|
||
|
onComplete(...args);
|
||
|
}
|
||
|
|
||
|
return result;
|
||
|
};
|
||
|
}
|