retry<T> function

Future<T> retry<T>(
  1. YoutubeHttpClient? client,
  2. FutureOr<T> function()
)

Run the function each time an exception is thrown until the retryCount is 0.

Implementation

Future<T> retry<T>(
  YoutubeHttpClient? client,
  FutureOr<T> Function() function,
) async {
  var retryCount = 5;

  // ignore: literal_only_boolean_expressions
  while (true) {
    try {
      return await function();
      // ignore: avoid_catches_without_on_clauses
    } on Exception catch (e) {
      if (client != null && client.closed) {
        throw HttpClientClosedException();
      }

      retryCount -= getExceptionCost(e);
      if (retryCount <= 0) {
        rethrow;
      }
      await Future.delayed(const Duration(milliseconds: 500));
    }
  }
}