Hi I want to write my owm promisified retry function, spesifically the p-retry function that returns promise (not callback).
And I want to make a generic function that promisifies for example readFile function as an argument that retry function takes.
I'm writing it in typescript for more readable code. However, I'm facing an issue because when I pass the readFile function it throws an error this type of argument is not assignable to ...
Can you give me a hint please what am I doing wrong? I'm kinda beginner with typescript so I know it's not probably writen exactly how it should be...
const promisifyRetryFun = <T>(fn: (
input: () => Promise<T> | T ,
options?: { retries?: number }
) => Promise<T>) =>
async (path: string, maxRetries: number) => {
let attemptCount = 0
const result = <T>(): Promise<T> =>
new Promise((resolve, reject) => {
(fun: (path: string, callback: (error: Error, result: any) => void) => void) => {
fun(path, async (error, result) => {
if(attemptCount >= maxRetries) reject(error)
if(error) fn(await result(), {retries: maxRetries})
attemptCount++
resolve(result)
})
}
})
fn(await result(), {retries: maxRetries})
}
void (async () => {
try{
const retr = promisifyRetryFun(retry(readFile))
const data = await (retr('./txt.txt', 3))
} catch (error) {
console.error({ error })
}
})()