withRetry method
Executes the underlying future operation with a structured retry mechanism.
If the operation throws an unexpected exception, it will pause for the allocated
duration and retry until maxAttempts is exhausted. If all attempts fail, the final
exception is thrown downstream.
Example:
final result = await Result.guardAsync(
() => (() => http.get(uri)).withRetry(
RetryOptions(maxAttempts: 3, delay: Duration(seconds: 1))
),
);
Implementation
Future<T> withRetry([RetryOptions options = const RetryOptions()]) async {
int attempts = 0;
Duration currentDelay = options.delay;
while (true) {
try {
attempts++;
return await this();
} catch (exception) {
if (attempts >= options.maxAttempts) {
rethrow;
}
await Future.delayed(currentDelay);
currentDelay = currentDelay * options.backoffFactor;
}
}
}