Skip to content

fix(retry): pass error to delay callback#1759

Open
dohwi wants to merge 2 commits into
toss:mainfrom
dohwi:fix/retry-delay-error-parameter
Open

fix(retry): pass error to delay callback#1759
dohwi wants to merge 2 commits into
toss:mainfrom
dohwi:fix/retry-delay-error-parameter

Conversation

@dohwi

@dohwi dohwi commented Jun 9, 2026

Copy link
Copy Markdown

Summary

Closes #1707. Enable error-aware retry intervals by passing the error object to the delay callback, consistent with how shouldRetry already receives it.

Changes

  • delay callback now receives (attempts: number, error: unknown) — error is passed as the second argument alongside the attempt number
  • delay?: number | ((attempts: number, error: unknown) => number) type updated in both the RetryOptions interface and implementation
  • Added a test verifying delay receives the correct error on retry
  • Updated documentation in all 4 languages (en, ko, ja, zh_hans) with error-aware delay examples

Test results

✓ src/function/retry.spec.ts (10 tests) 175ms
  ✓ should pass error to delay function
  ✓ should retry with a dynamic delay function (backward compat)
  ... 8 more all passing

Full suite: 560 test files, 4482 tests passed, 0 failures.

The `delay` option now receives the error object as its second
argument, matching the `shouldRetry` callback signature. This allows
error-aware retry intervals (e.g., respecting Retry-After headers).

Closes toss#1707
@dohwi dohwi requested a review from raon0211 as a code owner June 9, 2026 08:11
Copilot AI review requested due to automatic review settings June 9, 2026 08:11
@dohwi dohwi requested a review from dayongkr as a code owner June 9, 2026 08:11
@vercel

vercel Bot commented Jun 9, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
es-toolkit Ready Ready Preview, Comment Jun 9, 2026 8:44am

Request Review

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

Adds error-aware retry delays by extending the delay callback signature to receive the thrown error, with updated docs and tests to reflect the new behavior.

Changes:

  • Extend RetryOptions.delay function type to (attempts, error) => number and pass the error when computing delay.
  • Add a unit test verifying the error is provided to the delay callback.
  • Update English + localized docs with an error-based delay example and updated type signatures.

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
src/function/retry.ts Updates delay callback type and passes the caught error into delay computation; updates JSDoc.
src/function/retry.spec.ts Adds test coverage for passing the error to delay.
docs/reference/function/retry.md Documents error-aware delay usage and updates delay option signature.
docs/zh_hans/reference/function/retry.md Same as above (Simplified Chinese).
docs/ko/reference/function/retry.md Same as above (Korean).
docs/ja/reference/function/retry.md Same as above (Japanese).

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/function/retry.ts
Comment on lines 145 to 149
throw err;
}

const currentDelay = typeof delay === 'function' ? delay(attempts) : delay;
const currentDelay = typeof delay === 'function' ? delay(attempts, error) : delay;
await delayToolkit(currentDelay);
Comment thread src/function/retry.ts Outdated
* @default 0
* @example
* delay: (attempts) => attempt * 50
* delay: (attempts, error) => error.status === 429 ? 10000 : attempt * 50
Comment thread src/function/retry.ts Outdated
@@ -90,6 +90,9 @@ export async function retry<T>(func: () => Promise<T>, retries: number): Promise
* // Retry a function with a delay increasing linearly by 50ms per attempt
* retry(() => fetchData(), { delay: (attempts) => attempt * 50, retries: 5 });
Comment on lines +61 to +64
delay: (_attempts, error) => {
if (error instanceof ResponseError && error.status === 429) {
return error.retryAfter * 1000;
}
@dohwi

dohwi commented Jun 9, 2026

Copy link
Copy Markdown
Author

Thanks @copilot-pull-request-reviewer for the review.

  • Comment on error variable (line 149): The error variable is declared at line 131 in the outer function scope (let error;) and assigned in the catch block (error = err;). By line 149, error is always populated — the catch parameter err and the outer variable error are distinct. This is a false positive.

  • Comments on attempt vs attempts (lines 10, 91): Good catch — fixed in the latest commit.

  • ResponseError not defined in docs example: Fixed — replaced with a self-contained type narrowing using (error as { status?: number }).

- Fix `attempt` → `attempts` in JSDoc `@example` blocks
- Replace `ResponseError` with self-contained type narrowing in all doc examples
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Suggestion: Retry's delay with error parameter like shouldRetry

2 participants