Skip to content

Conversation

@MrBartusek
Copy link

@MrBartusek MrBartusek commented Jan 23, 2026

Fixes: #536

This PR adds retry: FetchRetry to FetchContext, which effectively adds the ability to get the current retry attempt. Original snippets call for context.options.retryAttempt, but it seems odd to pass such context as options. That would also mean library consumers would be able to manually enter retryAttempt. I've instead added a new object to the FetchContext

Exponential backoff can now be implemented as such:

$fetch("url", {
  retry: 5,
  retryDelay: exponentialBackoff() // 1s, 2s, 4s, 8s ...
}
    
function exponentialBackoff(baseDelay = 1_000) {
  return (context: FetchContext<any, 'json'>) => {
    return baseDelay * 2 ** context.retry.attempt
  }
}

I think this would be classified as a breaking change (?) since I removed the current behaviour where context.options.retry decreases on each attempt. It seems like a bug, don't think that options should mutate on each attempt - but I can add it back

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.

Add retryAttempt to FetchContext

1 participant