-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Description
This issue was originally filed by sas...@chromium.org
Dart currently does not recognize 'await' unless it is used inside an async method:
$ cat foo.dart
import 'dart:async';
String foo() async => 'foo';
void main() { print(await foo()); }
$ dart --enable-async foo.dart
'file:///Users/sascha/src/dart_async/foo.dart': error: line 3 pos 27: ')' expected
void main() { print(await foo()); }
^
When main() gets declared as async, Dart will execute the code just fine. So it seems that the 'await' statement currently gets only recognized when used inside an async method. Is this intentional? Arguably, the point of the 'await' statement is allowing synchronous code to wait for async results (futures). So, wouldn't the main utility of the 'await' statement be inside methods that are not declared async?
If the reason for the current behavior was backwards compatibility, couldn't the language feature get triggered by importing 'dart:async'? However, in case there's actually a deeper reason why 'await' can/should not work from inside non-async methods, it might be good to change the parser so it emits a better error message.
$ dart --version
Dart VM version: 1.8.5 (Tue Jan 13 13:04:11 2015) on "macos_ia32"