Skip to content

Custom retry logic #503

@stychu

Description

@stychu

Describe the feature

Having a retry on specific status codes is great but could we get a more customizable way of getting a retry to happen? I think this is a common use case to retry a request if the response doest match criteria.

Now I need to do custom wrapper logic to get such functionality.

//custom wrapper
export async function retryCall<T>({
  callback,
  maxRetries = 5,
  isResponseMatch,
  delayMs = 1000,
}: {
  maxRetries?: number;
  isResponseMatch: (data: T) => boolean;
  callback: () => Promise<T>;
  delayMs?: number;
}): Promise<T> {
  for (let attempt = 1; attempt < maxRetries; attempt++) {
    try {
      const response = await callback();

      if (isResponseMatch(response)) {
        return response;
      }
    } catch (error) {
      throw error;
    }

    await new Promise((resolve) => setTimeout(resolve, delayMs));
  }

  return await callback();
}
await retryCall({
      callback: () => ofetch(...),
      isResponseMatch: (data) => data === 'success',
    })

Additional information

  • Would you be willing to help implement this feature?

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions