GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — add completion for promise context by Kingwl · Pull Request #32101 · microsoft/TypeScript

Conversation

@Kingwl

@Kingwl Wenlu Wang (Kingwl) commented Jun 26, 2019 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

Fixes #31450

This PR added a new SymbolOriginInfo for Promise and create await on createCompletionEntry if the node is in AwaitContext.
I'm not sure the current insertText behavior is totally correct, and as the result of completionOfAwaitPromise3.ts, everything seems ok, but I cannot do it in my local vscode

unsupported:

  • add async modifier to container function
  • top level await

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@Kingwl
add completion for promise context
4cc0696
@andrewbranch Andrew Branch (andrewbranch) self-assigned this Jun 26, 2019
@andrewbranch Andrew Branch (andrewbranch) mentioned this pull request Jun 26, 2019
Auto-insert await for property accesses on Promise #31450
Closed
sheetalkamat
Sheetal Nandi (sheetalkamat) reviewed Jun 26, 2019
View reviewed changes
Comment thread src/services/completions.ts Resolved

Uh oh!

There was an error while loading. Please reload this page.

andrewbranch
Andrew Branch (andrewbranch) reviewed Jul 1, 2019
View reviewed changes

@andrewbranch Andrew Branch (andrewbranch) left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My suspicion is that most of the time when these completions appear, my desired fix is like this:

async function fetchThings(fetchOptions: RequestInfo): Thing[] {
  const rawJson = (await fetch(fetchOptions)).json(); // accidentally a Promise<any>
  return deserializeThingsResponse(rawJson);
}

// should become

async function fetchThings(fetchOptions: RequestInfo): Thing[] {
  const rawJson = await (await fetch(fetchOptions)).json();
  return deserializeThingsResponse(rawJson);
}

If I have a variable declaration a) without a declared type, b) inside an async function, c) whose resolved type is a Promise, and d) I never successfully access any members of that Promise, then chances are, I meant to use await at the declaration site, not the use site.

Of course, that’s more semantic criteria to check. It also increases the likelihood that the codefix will take place offscreen. Let me see if I can try to find some data on how frequently we see parenthesized await expressions vs. as the initializer of a variable declaration.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@andrewbranch

Andrew Branch (andrewbranch) commented Jul 2, 2019 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

Ok, so in our user tests (123,359 TS/JS/TSX/JSX files), I looked for the forms:

  • any await expression
  • const x = await something (AwaitExpression as VariableDeclaration initializer)
  • (await foo).bar (AwaitExpression as expression of ParenthesizedExpression as expression of PropertyAccessExpression)

The results were:

  • total await: 3,983 occurrences in 358 files
  • initializer: 1,391 occurrences in 271 files (35% of all awaits)
  • parenthesized then property access: 76 occurrences in 26 files (2% of all awaits)

This isn't a huge sample size (of files that actually contain await)—but 2% vs. 35% is a big enough gap that I’m skeptical that this is the refactor users expect when choosing a completion. /cc Daniel Rosenwasser (@DanielRosenwasser)

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@Kingwl

Wenlu Wang (Kingwl) commented Jul 3, 2019

Copy link
Copy Markdown
Contributor Author

That should be a quick fix and trigger by type mismatch.
The difference is property access is usual when you are writing some fresh code.

I‘m not sure but I think you cannot find too many property access is caused people always write the following code rather than parens and parens:

async function fetchThings(fetchOptions: RequestInfo): Thing[] {
  const resp = await fetch(fetchOptions)
  const rawJson = await resp.json();
  return deserializeThingsResponse(rawJson);
}
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@andrewbranch

Andrew Branch (andrewbranch) commented Jul 3, 2019

Copy link
Copy Markdown
Member

Sure, maybe my example wasn’t very good. But I think there will generally be cases like

declare function someAsyncThing(): Promise<{ foo: string }>;

async function whatever() {
  const x = someAsyncThing(); // meant to await
  x./*|*/
}

and if we complete foo there, it would be better to place the await on await someAsyncThing(). There are many other cases where that wouldn’t work, like if x was a function parameter, or if x was already used elsewhere, or if the property access was occurring on a non-identifier expression. In those cases, I think this completion behavior makes sense, but I want to consider and get feedback on whether it makes sense to try to offer the declaration site await where relevant.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@andrewbranch
Andrew Branch (andrewbranch) requested a review from Daniel Rosenwasser (DanielRosenwasser) July 3, 2019 15:25
@andrewbranch Andrew Branch (andrewbranch) mentioned this pull request Jul 3, 2019
Improve error messages for potentially missing 'await' #32239
Merged
@Kingwl

Wenlu Wang (Kingwl) commented Jul 4, 2019

Copy link
Copy Markdown
Contributor Author

It's also a bit weird that we request autocomplete here and the leading code has been modified.

if x was already used elsewhere

And I'm not sure that is acceptable what request find all reference in autocomplete?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@andrewbranch

Andrew Branch (andrewbranch) commented Jul 9, 2019

Copy link
Copy Markdown
Member

I'm not sure that is acceptable what request find all reference in autocomplete?

I’m working on something similar for the codefix scenario:

add-await

Over the next couple days I’ll be testing this out and seeing how the performance is. We don’t need to do quite a full find-all-references, and the scope should be limited to the source file at largest.

I think we should get this merged, and if it seems reasonable to add the initializer await, I can make it pull in the same code I’m working on now.

Have you addressed Sheetal’s feedback?

Kingwl reacted with rocket emoji
All reactions
  • 🚀 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@andrewbranch

Andrew Branch (andrewbranch) commented Jul 10, 2019 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

completionOfAwaitPromise3.ts, everything seems ok, but I cannot do it in my local vscode

Yeah, it doesn’t work for me either—I stepped through on the VS Code side and confirmed the response from TSServer looks good, and I couldn’t find where it was getting filtered out. It seems like it makes it all the way out of the typescript-language-features extension. Will likely need some help from the VS Code folks to figure this one out, but I don’t think it’s a problem for this PR.

👇 stupid MacBook trackpad clicked the button while I was typing 😑

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@andrewbranch Andrew Branch (andrewbranch) closed this Jul 10, 2019
@andrewbranch Andrew Branch (andrewbranch) reopened this Jul 10, 2019
Wenlu Wang (Kingwl) added 2 commits July 11, 2019 00:55
@Kingwl
check insert text inside add symbol helper
1c2bb1c
@Kingwl
Merge branch 'master' into completion_promise
df4bce1
andrewbranch
Andrew Branch (andrewbranch) reviewed Jul 10, 2019
View reviewed changes
Comment thread src/services/completions.ts Outdated
function addSymbol (symbol: Symbol) {
if (insertAwait && !symbolToOriginInfoMap[getSymbolId(symbol)]) {
symbolToOriginInfoMap[getSymbolId(symbol)] = { kind: SymbolOriginInfoKind.Promise };
if (preferences.includeCompletionsWithInsertText) {

@andrewbranch Andrew Branch (andrewbranch) Jul 10, 2019

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be something like if (!insertAwait || preferences.includeCompletionsWithInsertText)? Seems like addSymbol would now reject all completions if the preference is disabled 🙃

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@Kingwl

Wenlu Wang (Kingwl) commented Jul 10, 2019 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor Author

I still felt it's hard to get an await-missing expression when you are coding.
But it's not a big deal.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@Kingwl
fix incorrect branch
9cbac99
andrewbranch
Andrew Branch (andrewbranch) approved these changes Jul 10, 2019
View reviewed changes
@andrewbranch

Andrew Branch (andrewbranch) commented Jul 10, 2019

Copy link
Copy Markdown
Member

Looks like just a lint error now.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

sheetalkamat
Sheetal Nandi (sheetalkamat) reviewed Jul 10, 2019
View reviewed changes
Comment thread src/services/completions.ts Resolved

Uh oh!

There was an error while loading. Please reload this page.

@Kingwl
avoid completions with includeCompletionsWithInsertText perferences
e4e7aba
@Kingwl

Wenlu Wang (Kingwl) commented Jul 17, 2019

Copy link
Copy Markdown
Contributor Author

Hope I have not delay your release.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

sheetalkamat
Sheetal Nandi (sheetalkamat) requested changes Jul 17, 2019
View reviewed changes
Comment thread src/services/completions.ts Outdated
origin: SymbolOriginInfo | undefined,
kind: CompletionKind,
): CompletionEntryDisplayNameForSymbol | undefined {
const needsConvertAwait = !!(origin && originIsPromise(origin));

@sheetalkamat Sheetal Nandi (sheetalkamat) Jul 17, 2019

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this needs to be passed around since this check can easily be performed as usage sites of needsConvertAwait

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread src/services/completions.ts Resolved

Uh oh!

There was an error while loading. Please reload this page.

@Kingwl
avoid useless parameter
f527388
sheetalkamat
Sheetal Nandi (sheetalkamat) approved these changes Jul 18, 2019
View reviewed changes
@andrewbranch
Andrew Branch (andrewbranch) merged commit 9942c60 into microsoft:master Aug 26, 2019
@Kingwl
Wenlu Wang (Kingwl) deleted the completion_promise branch August 27, 2019 02:27
Tim Suchanek (timsuchanek) pushed a commit to timsuchanek/TypeScript that referenced this pull request Sep 11, 2019
@Kingwl @timsuchanek
add completion for promise context (microsoft#32101)
493923c
* add completion for promise context

* check insert text inside add symbol helper

* fix incorrect branch

* avoid completions with includeCompletionsWithInsertText perferences

* avoid useless parameter
@microsoft Microsoft (microsoft) locked as resolved and limited conversation to collaborators Oct 21, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Reviewers

@andrewbranch Andrew Branch (andrewbranch) andrewbranch approved these changes
@sheetalkamat Sheetal Nandi (sheetalkamat) sheetalkamat approved these changes
@DanielRosenwasser Daniel Rosenwasser (DanielRosenwasser) Awaiting requested review from DanielRosenwasser

Uh oh!

There was an error while loading. Please reload this page.

Assignees

@andrewbranch Andrew Branch (andrewbranch)

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Auto-insert await for property accesses on Promise

Uh oh!

There was an error while loading. Please reload this page.

3 participants

@Kingwl @andrewbranch @sheetalkamat
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.