retry<T> static method

Future<T> retry<T>(
  1. RetryFuture<T> future, {
  2. int tries = 1,
  3. Duration delay = const Duration(seconds: 1),
  4. RetryCondition? retryCondition,
})

Returns a Future that will retry future while it throws for a maximum of tries times with delay in between. If all the attempts throws, the future will throw a List of the thrown objects by the future.

Implementation

static Future<T> retry<T>(
  RetryFuture<T> future, {

  /// number of total tries (first try + retries)
  int tries = 1,
  Duration delay = const Duration(seconds: 1),
  RetryCondition? retryCondition,
}) async {
  List<Object> errors = [];
  while (tries-- > 0) {
    try {
      return await future(tries, errors);
    } catch (error) {
      logger.fine('[Retry] Caught error ${error}...');
      errors.add(error);
      if (!(retryCondition?.call(tries, errors) ?? true)) break;
    }
    if (tries > 0) {
      logger.fine('[Retry] Waiting ${delay}...');
      await Future<dynamic>.delayed(delay);
    }
  }
  throw errors;
}