Skip to content

Instantly share code, notes, and snippets.

@sethladd
Created February 27, 2013 16:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save sethladd/5049080 to your computer and use it in GitHub Desktop.
Save sethladd/5049080 to your computer and use it in GitHub Desktop.
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