Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

223
Vistas
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
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda