question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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 (https://github.com/microsoft/TypeScript/issues/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.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
RyanCavanaughcommented, Mar 3, 2022

Not a huge fan. I think in general, if I complete something that requires await, I want to be told that I’m not currently in an async function, and decide what to do about it – maybe I’m in a code path that is necessarily sync, maybe not, and the quick-fix to add missing async is very readily available. Changing a function’s color is not a decision to be made lightly and I don’t like it when editors make big decisions on my behalf; making those changes should be low-friction, but not done with consulting the user.

0reactions
philkunzcommented, Mar 16, 2022

auto adding async keyword is a bad idea.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why must "await" be inside an "async" method? - Stack Overflow
I'm confused. When I use the await keyword, the calling thread will be suspended and wait for task completion. So once await is...
Read more >
Async/Await - Best Practices in Asynchronous Programming
Figure 2 illustrates that exceptions thrown from async void methods can't be caught ... have it await an async Task method that contains...
Read more >
Using Async and Await in C# - Aaron Bos
The async modifier is used to signify that a method, ... you will see a compiler warning if the async method doesn't await...
Read more >
Async And Await In C# - C# Corner
In this article, you'll learn what C# async and C# await keywords are ... first Method1 and it will wait for the completion...
Read more >
Meet async/await in Swift - WWDC21 - Videos - Apple Developer
Swift now supports asynchronous functions — a pattern commonly known as async / await. Discover how the new syntax can make your code......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found