Skip to content

Instantly share code, notes, and snippets.

@mkmik
Last active August 5, 2019 17:35
Show Gist options
  • Save mkmik/933864db9de99d84b9697399ed4cc939 to your computer and use it in GitHub Desktop.
Save mkmik/933864db9de99d84b9697399ed4cc939 to your computer and use it in GitHub Desktop.
test
import 'dart:math';
var random = Random.secure();
Future<int> rand() {
return Future.delayed(new Duration(seconds: 1), () { return random.nextInt(200); });
}
Future<int> get() async {
print("beginning to save");
int n = await rand();
if (n < 100) {
throw "unlucky";
}
return n - 100;
}
Future<T> persevere<T>(Future<T> fn()) async {
while(true) {
try {
return await fn();
} catch(e) {
print('error ${e}, retrying');
}
}
}
void main() {
persevere(get).then((int a) {
print('ok ${a}');
});
print("after then");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment