wrapping a generic function in typescript

i'm trying to write a wrapper to automatically handle loading state when necessary. my idea is to wrap my function definitions in this function busyable which then returns a function that precedes and succeeds the callback passed in by toggling busy state. something like this:
const busyable = (fn: (...args: any) => Promise<void>) => {
return async (...args: any) => {
busy = true;
await fn(...args);
busy = false;
};
};
const busyable = (fn: (...args: any) => Promise<void>) => {
return async (...args: any) => {
busy = true;
await fn(...args);
busy = false;
};
};
something is off (likely function parameters), and it seems like my function might be getting run twice. any help is appreciated.
4 Replies
jack
jack2y ago
oh wait, it is actually working, though i'd love to hear of any potential improvements
Valhalaar
Valhalaar2y ago
Is your intended usage for this immediately following a variable declaration for busy? Otherwise this is setting a global busy and will run into conflicts with other things using this function. Even if you do put it right after a busy variable, you only get one of those per lexical scope It seems like you’re trying to kinda reinvent the promise here
jack
jack2y ago
its globally declared, using for render logic; the idea was primarily to use it for disabled state on buttons yea i think im over complicating this i suppose
Valhalaar
Valhalaar2y ago
so you want all buttons to be disabled of any kind of (wrapped) async operation is going on? not just disabled based on things each button controls respectively?
Want results from more Discord servers?
Add your server