Repository navigation
feat: add retry() helper #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| /** Retry a flaky async operation a few times before giving up. */ | ||
| export async function retry<T>(fn: () => Promise<T>, attempts = 3): Promise<T> { | ||
| let lastError: unknown | ||
| for (let i = 0; i < attempts; i++) { | ||
| try { | ||
| return await fn() | ||
| } catch (e) { | ||
| lastError = e | ||
| } | ||
| } | ||
| throw new Error(`retry failed after ${attempts} attempts: ${String(lastError)}`) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ❌ Error handling / no-try-catch New thrown exception added. New fallible functions should return Result<T, E> from neverthrow using err(...)/ok(...) instead of throwing. Suggested fix: Return err(...) from neverthrow and update the function signature to return Result<T, E> or ResultAsync<T, E>. |
||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
❌ Error handling / no-try-catch
New try/catch block added in a new file. The codebase is migrating to neverthrow; new fallible code should return Result<T, E> instead of using try/catch.
Suggested fix: Refactor to accept a function returning ResultAsync<T, E> and use .orElse() for retries instead of try/catch.