`await` completions should auto-add an `async` modifier to containing method
See original GitHub issueLet’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 await
ing that function. Comparatively, there’s relatively little harm in marking a Promise
-returning function async
.
Issue Analytics
- State:
- Created 2 years ago
- Comments:7 (5 by maintainers)
Top GitHub Comments
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 missingasync
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.auto adding async keyword is a bad idea.