Skip to content

Should await work in non-async methods? #22269

@DartBot

Description

@DartBot

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"

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-vmUse area-vm for VM related issues, including code coverage, and the AOT and JIT backends.closed-duplicateClosed in favor of an existing reporttype-enhancementA request for a change that isn't a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions