Skip to content

Instantly share code, notes, and snippets.

@cdmunoz
Created May 30, 2023 03:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cdmunoz/697f3ceaaf89eb17524b3a22231e0e7a to your computer and use it in GitHub Desktop.
Save cdmunoz/697f3ceaaf89eb17524b3a22231e0e7a to your computer and use it in GitHub Desktop.
typedef FunctionOnRetry = Function(
dynamic ex,
int retryCount,
int retryInMilliseconds,
);
Future<void> onErrorRetry({
required Future Function() doIt,
required FunctionOnRetry onRetry,
required int maxRetries,
required int delayMilliseconds,
int retryCount = 0,
}) async {
try {
await doIt();
} catch (ex) {
if (retryCount < maxRetries) {
final nextCount = retryCount + 1;
final retryInMilliseconds = delayMilliseconds * nextCount;
await Future.delayed(Duration(milliseconds: retryInMilliseconds));
onRetry(ex, nextCount, retryInMilliseconds);
await onErrorRetry(
doIt: doIt,
onRetry: onRetry,
maxRetries: maxRetries,
retryCount: nextCount,
delayMilliseconds: delayMilliseconds,
);
} else {
rethrow;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment