Created
February 27, 2013 16:10
-
-
Save sethladd/5049080 to your computer and use it in GitHub Desktop.
This file contains 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:io'; | |
import 'dart:async'; | |
import 'dart:math'; | |
// The following function is part of a third-party package that you can't touch. | |
Future doCoolStuff() { | |
var completer = new Completer(); | |
// Sometimes bad things happen. | |
if (new Random().nextBool()) { | |
new Timer(100, (t) { | |
// how does a consumer of doCoolStuff catch this? | |
throw new Exception('Oops, something bad happened'); | |
}); | |
} | |
// Do some work for a second. | |
new Timer(1000, (t) { | |
print('good timer completed'); | |
completer.complete('We did cool things'); | |
}); | |
return completer.future; | |
} | |
// User side code following. | |
main() { | |
try { | |
doCoolStuff() | |
.then((r) => print(r)) | |
.catchError((e) { | |
print('caught from caughtError'); | |
print(e); | |
}); | |
} catch (e) { | |
print("caught in catch"); | |
print(e); | |
} | |
print('Done!'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment