Created
August 5, 2019 17:30
-
-
Save mkmik/f7f11bea0e87dd8ca7dfbfc2dccfe20f to your computer and use it in GitHub Desktop.
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
| 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