retry method

Future<Result<T, AppError>> retry({
  1. int times = 3,
  2. Duration delay = const Duration(seconds: 1),
})

Retry the operation on error times times with optional delay.

Usage:

Future<Result<User, AppError>> getUser() {

Implementation

//   return api.getUser().retry(times: 2, delay: Duration(milliseconds: 800));
// }
Future<Result<T, AppError>> retry({
  int times = 3,
  Duration delay = const Duration(seconds: 1),
}) async {
  late Result<T, AppError> result;

  for (int i = 0; i < times; i++) {
    result = await this;

    if (result.isSuccess) return result;

    await Future.delayed(delay);
  }

  return result;
}