Skip to content

await completions should auto-add an async modifier to containing method #48093

Open
@DanielRosenwasser

Description

@DanielRosenwasser

Let's say you have a function like the following:

function foo() {
   /*$*/
}

If you type the word await, we should consider adding the async keyword to the beginning of the function declaration.

The motivation here was another snippet completion for object literal completions that @gabritto is working on (#46590) where it's not clear whether you really want a method to be async or not. For example, you might not want to make a function async if it's just going to return a Promise.

interface Foo {
  doIt(): Promise<any>;
}

const obj: Foo = {
  doIt() {
    return fetch(...);
  }
}

Because there's not enough context, even with the Promise annotation, some on the team would prefer to be conservative and not complete this as async, instead relying on later editor functionality to make a function async.

Today, if a person does wants to add await to a function that doesn't have an async modifier, we provide a quick fix to do this.

interface Foo {
  doIt(): Promise<any>;
}

const obj: Foo = {
  doIt() {
//~~~~
//
// Did you mean to mark this function as 'async'?

    await fetch(...);
//  ~~~~~
//
//   'await' expressions are only allowed within async functions and at the top levels of modules.
//   Quick fix: Add async modifier to containing function.
  }
}

This quick fix is nice, but the idea of the suggestion in this issue here is to just do the same thing as the quick fix upon completion.

Now are there any reasons to not do this?

Mostly that the idea of being "conservative" in when we decide to make a method async is not being applied here. A person can accidentally make their functions async, and that might not be desirable either, especially if any callers are not awaiting that function. Comparatively, there's relatively little harm in marking a Promise-returning function async.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Domain: Completion ListsThe issue relates to showing completion lists in an editorIn DiscussionNot yet reached consensusSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions