Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

221
Views
Make p-retry function with promises

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 })
  }
})()
about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!