As far as I understand it should not be possible to await functions like void foo() async {}
, because they return no future that could be awaited. But I noticed when I assign them to a FutureOr<void> Function()
field it is possible to await them. The following example prints: PRE, FUNC, POST
import 'dart:async';
void func() async {
await Future.delayed(const Duration(seconds: 2));
print('FUNC');
}
Future<void> main() async {
final FutureOr<void> Function() t = func;
print('PRE');
await t();
print('POST');
}
I would expect it to print: PRE, POST, FUNC
Because the function func
cannot be await (because it returns void) and therefore the await t()
should not wait for the completion of the func
function.
I observed the same behavior with final dynamic Function() t = func;
I'm not sure if this behavior is intended or if I just don't understand void async functions?