Skip to content

async should be added when await is used #9084

Description

@nickserv

Using await in a function that isn't async is illegal in JavaScript, so Prettier currently fails to format with a parser error. However, Prettier should be able to detect an await inside a function that doesn't have async and add it when formatting for convenience and to prevent confusion.

Prettier 2.1.1
Playground link

Input:

async function depenency() {}

function dependent() {
  await dependency();
}

Output:

SyntaxError: Unexpected token, expected ";" (4:9)
  2 | 
  3 | function dependent() {
> 4 |   await dependency()
    |         ^
  5 | }

Expected behavior:

async function depenency() {}

async function dependent() {
  await dependency();
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    locked-due-to-inactivityPlease open a new issue and fill out the template instead of commenting.

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions