Advertisement
Guest User

Untitled

a guest
Aug 17th, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.53 KB | None | 0 0
  1. import 'dart:math';
  2.  
  3. var random = Random.secure();
  4.  
  5. Future<int> rand() {
  6. return Future.delayed(new Duration(seconds: 1), () { return random.nextInt(200); });
  7. }
  8.  
  9. Future<int> get() async {
  10. print("beginning to save");
  11. int n = await rand();
  12. if (n < 100) {
  13. throw "unlucky";
  14. }
  15. return n - 100;
  16. }
  17.  
  18. Future<int> persevere() async {
  19. while(true) {
  20. try {
  21. return await get();
  22. } catch(e) {
  23. print('error ${e}, retrying');
  24. }
  25. }
  26. }
  27.  
  28. void main() {
  29. persevere().then((int a) {
  30. print('ok ${a}');
  31. });
  32. print("after then");
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement