Skip to content

Instantly share code, notes, and snippets.

@whhone
Last active October 2, 2020 15:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save whhone/abda584f6e654285e2a7533e980304dd to your computer and use it in GitHub Desktop.
Save whhone/abda584f6e654285e2a7533e980304dd to your computer and use it in GitHub Desktop.
// .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