I want to create a wrapper function for an existing function in TypeScript.
The wrapper function could start some other process and clean it up after finishing the main ("callback") function passed to the wrapper.
This can be done using approaches like shown here. However, these solutions do not allow me to specify additional options that can be passed to the wrapper itself.
How would I go about doing that?
My starting point was:
export const wrap = async <T>(
callback: () => T | Promise<T>,
options?: { foo?: string | undefined },
): Promise<T> => {
let ret;
// begin
if (options.foo) {
// do something
}
try {
ret = await callback();
} catch (e) {
throw e;
} finally {
// cleanup
}
return ret;
};
This would not let me add arguments to callback(). I can use ...args, but how would I specify both ...args and options?