Skip to content

Instantly share code, notes, and snippets.

@fengyfei
Created May 21, 2019 12:04
Show Gist options
  • Save fengyfei/8365b5a791bfdda2925fe2071a1595a1 to your computer and use it in GitHub Desktop.
Save fengyfei/8365b5a791bfdda2925fe2071a1595a1 to your computer and use it in GitHub Desktop.
[dart:async] Future Chain
import 'dart:core';
import 'dart:async';
Future<bool> exception = Future<bool>.delayed(Duration(seconds: 1), () {
print('Somehow, a exception happend...');
throw 'Just a Exception, freeze!';
});
Future procedure = Future(() {
return exception
.then((val) {
print('Nonsense');
})
.catchError((err) {
print('Exception happend, rethrow it');
throw err;
});
});
main() {
procedure
.then((val) {
print('Succeed, $val');
})
.catchError((err) {
print('A Exception captured, try to recovering...');
return Future.value(1024);
})
.then((val) {
print('Recovered from exception, val is $val');
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment