feat: orElse callback when retries is exhausted#213
Open
drown0315 wants to merge 1 commit intogoogle:masterfrom
Open
feat: orElse callback when retries is exhausted#213drown0315 wants to merge 1 commit intogoogle:masterfrom
drown0315 wants to merge 1 commit intogoogle:masterfrom
Conversation
Member
|
I could use this as: final msg = await retry(
getMessage,
retryIf: (e) => e is TimeoutException,
orElse: (e) => 'failed to get data',
);But I could also do: final msg = await retry(
getMessage,
retryIf: (e) => e is TimeoutException,
).catchError((e) => 'failed to get data');Maybe, instead of creating a new
I think a section in the Maybe, even a hint in the documentation for the /// Call [fn] retrying so long as [retryIf] return `true` for the exception
/// thrown, up-to [maxAttempts] times.
///
/// Defaults to 8 attempts, sleeping as following after 1st, 2nd, 3rd, ...,
/// 7th attempt:
/// 1. 400 ms +/- 25%
/// 2. 800 ms +/- 25%
/// 3. 1600 ms +/- 25%
/// 4. 3200 ms +/- 25%
/// 5. 6400 ms +/- 25%
/// 6. 12800 ms +/- 25%
/// 7. 25600 ms +/- 25%
///
/// ```dart
/// final response = await retry(
/// // Make a GET request
/// () => http.get('https://google.com').timeout(Duration(seconds: 5)),
/// // Retry on SocketException or TimeoutException
/// retryIf: (e) => e is SocketException || e is TimeoutException,
/// );
/// print(response.body);
/// ```
///
/// If no [retryIf] function is given this will retry any for any [Exception]
/// thrown. To retry on an [Error], the error must be caught and _rethrown_
/// as an [Exception].
///
/// To always produce a value, even when retries are failed or an exception not
/// matched by [retryIf] is encountered, combine [retry] with [Future.catchError].
/// ```dart
/// final body = await retry(
/// () => http.read('https://google.com').timeout(Duration(seconds: 5)),
/// retryIf: (e) => e is SocketException || e is TimeoutException,
/// ).catchError((e) => 'failed to get data');
/// print(body);
/// ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
reference by #167