retry function

Future<Response> retry(
  1. Future<Response> fn(), {
  2. int count = 5,
})

Implementation

Future<Response> retry(Future<Response> Function() fn, {int count = 5}) async {
  final response = await fn();

  if (count == 0) {
    return response;
  }

  if (response.statusCode == 429 ||
      response.statusCode == 501 ||
      response.statusCode == 502 ||
      response.statusCode == 503) {
    await Future.delayed(const Duration(milliseconds: 1000));

    return retry(fn, count: count - 1);
  }

  return response;
}