My current function is functioning perfectly:
do {
try {
const { assets } = await myApi(arg1, arg2, arg3); <<--
return assets;
} catch (err: any) {
if (err.response.status == 429) {
await sleep(300);
console.log('Throttle occurred, sleeping for 300ms');
} else
throw (err);
}
}
while (true);
I aim to generalize this method and invoke it like so:
throttleHandler(myApi, ...args)
I made a few attempts but TypeScript raised some issues. I'm uncertain about the correctness of my approach and prefer not to use any
. It's important for me to ensure types are defined.
type ThrottleHandler<TOut> = (...args: any) => TOut;
async function throttleHandler<T>(promiseFunction: ThrottleHandler<T>, ...args: any): Promise<T> {
console.log("args", args, ...args)
do {
try {
return await promiseFunction(...args);
} catch (err: any) {
if (err.response.status == 429) {
await sleep(300);
console.log('Throttle occurred, sleeping for 300ms');
} else
throw (err);
}
}
while (true);
}