tryRun<T> static method

Future<T?> tryRun<T>(
  1. Future<T> asyncFunc(), {
  2. Duration? timeRetry = const Duration(milliseconds: 100),
  3. int retries = 3,
  4. CancellationToken? cancelToken,
  5. bool throwWhenException()?,
})

Implementation

static Future<T?> tryRun<T>(
  Future<T> Function() asyncFunc, {
  Duration? timeRetry = const Duration(milliseconds: 100),
  int retries = 3,
  CancellationToken? cancelToken,
  bool Function()? throwWhenException,
}) async {
  int attempts = 0;
  while (attempts <= retries) {
    attempts++;
    try {
      return await asyncFunc();
    } on OperationCanceledError catch (error) {
      throw error;
    } catch (error) {
      if (throwWhenException != null && throwWhenException()) {
        throw error;
      }
    }
    //delay to retry
    if (attempts < retries && timeRetry != null) {
      var future = CancellationTokenSource.register(
        cancelToken,
        Future.delayed(timeRetry),
      );
      await future;
    }
  }
  return null;
}