Last active
August 5, 2019 17:35
-
-
Save mkmik/933864db9de99d84b9697399ed4cc939 to your computer and use it in GitHub Desktop.
test
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<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