Skip to content

Instantly share code, notes, and snippets.

@mkmik
Created August 5, 2019 17:30
Show Gist options
  • Select an option

  • Save mkmik/f7f11bea0e87dd8ca7dfbfc2dccfe20f to your computer and use it in GitHub Desktop.

Select an option

Save mkmik/f7f11bea0e87dd8ca7dfbfc2dccfe20f to your computer and use it in GitHub Desktop.
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<int> persevere() async {
while(true) {
try {
return await get();
} catch(e) {
print('error ${e}, retrying');
}
}
}
void main() {
persevere().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