withRetry<T> function

Future<T> withRetry<T>(
  1. FutureOr<T> action(), {
  2. int retryTimes = 3,
  3. int retryIntervalMills = 500,
})

Implementation

Future<T> withRetry<T>(
  FutureOr<T> Function() action, {
  int retryTimes = 3,
  int retryIntervalMills = 500,
}) async {
  assert(retryTimes >= 0);
  assert(retryIntervalMills >= 0);
  int times = 0;
  while (true) {
    try {
      return await action();
    } catch (e) {
      times++;
      if (times >= retryTimes) {
        rethrow;
      }
      await Future.delayed(Duration(milliseconds: times * retryIntervalMills));
    }
  }
}