Last active
October 2, 2020 15:26
-
-
Save whhone/abda584f6e654285e2a7533e980304dd to your computer and use it in GitHub Desktop.
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
// .then block does not complete | |
Future<void> foo_then_bad() async { | |
final future = Future(() => print('foo_then_bad: started')) | |
.then((_) async => () async { | |
await Future.delayed(Duration(seconds: 1), null); | |
print('foo_then_bad: 111'); | |
await Future.delayed(Duration(seconds: 1), null); | |
print('foo_then_bad: 222'); | |
}); | |
return await future; | |
} | |
// .then block completes | |
Future<void> foo_then_good() async { | |
final future = Future(() => print('foo_then_good: started')) | |
.then((_) async { | |
await Future.delayed(Duration(seconds: 1), null); | |
print('foo_then_good: 111'); | |
await Future.delayed(Duration(seconds: 1), null); | |
print('foo_then_good: 222'); | |
}); | |
return await future; | |
} | |
// full async version, for reference | |
Future<void> foo_async() async { | |
final future = Future(() => print('foo_async: started')); | |
await future; | |
await Future.delayed(Duration(seconds: 1), null); | |
print('foo_async: 111'); | |
await Future.delayed(Duration(seconds: 1), null); | |
print('foo_async: 222'); | |
} | |
void main() async { | |
// Uncomment to test different versions. | |
await foo_then_bad(); | |
await foo_then_good(); | |
await foo_async(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment