Upon calling the following function, it returns a Promise<boolean>
:
const fnc = (i:number) : Promise<boolean> => Promise.resolve(true)
// Promise<boolean>
const res1 = errorHandler(errorPredicates.sdkError1, fnc, null, 4);
However, when I nest it with another error handler, the returned object changes to Promise<any>
:
// Promise<any>
const res2 = errorHandler(errorPredicates.sdkError1, errorHandler, null, errorPredicates.sdkError2, fnc, null, 4);
Below is an example of TypeScript code that replicates the issue. It is uncertain what is causing this problem, or if it is due to a TypeScript limitation. How can this typing issue be resolved?
type PromiseFn = (...args: any[]) => Promise<any>;
type UnwrappedReturnType<T extends PromiseFn> = T extends (...args: any) => Promise<infer R> ? R : any
type ThrottlerPredicate = (err: any) => boolean;
type ThrottlerPredicates = {
sdkError1: ThrottlerPredicate
sdkError2: ThrottlerPredicate
}
const errorPredicates: ThrottlerPredicates = {
sdkError1: (err) => err?.message?.search(`429:`) != -1,
sdkError2: (err) => err?.message?.search(`Request was throttled.`) != -1
}
async function errorHandler<ApiFn extends PromiseFn>(
predicate: ThrottlerPredicate,
promiseFunction: ApiFn,
thisArg: ThisParameterType<ApiFn>,
...args: Parameters<ApiFn>
): Promise<UnwrappedReturnType<ApiFn>> {
let errCount = 0
do {
try {
const promiseResult = await promiseFunction.call(thisArg, ...args)
return promiseResult
} catch (err: any) {
//console.error(err)
if (predicate(err)) {
if (errCount < 20)
++errCount;
var ms = 1500 * errCount
} else
throw (err);
}
}
while (true);
}
const fnc = (i:number) : Promise<boolean> => Promise.resolve(true)
// Promise<boolean>
const res1 = errorHandler(errorPredicates.sdkError1, fnc, null, 4);
// Promise<any>
const res2 = errorHandler(errorPredicates.sdkError1, errorHandler, null, errorPredicates.sdkError2, fnc, null, 4);