vercel / async-retry

Retrying made simple, easy and async
https://npmjs.com/async-retry
MIT License
1.85k stars 53 forks source link

Passing function as argument does not work #94

Closed tvld closed 2 years ago

tvld commented 2 years ago

This is most likely because I am not familiar enough with promises... but, how can I pass a function in a variable and then retry it if it fails the first time?:

// works ok:
await retry(
  async (bail) => {
    const res = await fetch('https://google.com')
  }
);

// does not work 
const Fn = fetch('https://google.com')

await retry(
  async (bail) => {
    const res = await Fn; // if fails first time, it is not called again
  }
);
tvld commented 2 years ago

Answering my own question, this will work:

const Fn =  async () => {
     return await fetch('https://google.com')
 }

const res = await retry(
   Fn
)