matheus-rodrigues00 / utils

✨ Library to commonly used cross-projects utilities methods ✨
17 stars 11 forks source link

Create retry Method #40

Open matheus-rodrigues00 opened 1 year ago

matheus-rodrigues00 commented 1 year ago

Implement a retry method that executes a promise-returning function repeatedly until it succeeds or reaches a maximum number of retries. Also allows specifying the interval between retry attempts. Example Usage:

async function retry<T>(
  promise_function: () => Promise<T>,
  max_retries: number,
  retry_interval_ms: number
): Promise<T> {
  // Implementation goes here
}
ahn0min commented 1 year ago

I felt that my understanding of Promise improved in the previous issue.

I think this issue is also related, so can I be greedy about the issue?

matheus-rodrigues00 commented 1 year ago

@ahn0min go ahead! 🚀

ahn0min commented 1 year ago

@matheus-rodrigues00 I ran into a problem.

I wonder what the criteria for success should be. If it resolves after the interval time, is this a success?

If so, shouldn't there be a not retry after that? And if I retry up to 3 times, should Promises 1 and 2 be canceled instead of running in the background?

matheus-rodrigues00 commented 1 year ago

@ahn0min I think you should run the Promise until it rejects. When it does reject you should run the promise over again after sleep the retry_interval_ms.

The criteria for success: The promise resolves, no matter the interval.

You shouldn't worry about cancelling Promises on background, because there will be none, since you will only jump to the next Promise call after the currently rejects.

Anyway, this idea is not written in stone, use your best judgment.