Created
May 21, 2019 12:04
-
-
Save fengyfei/8365b5a791bfdda2925fe2071a1595a1 to your computer and use it in GitHub Desktop.
[dart:async] Future Chain
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: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