-
Notifications
You must be signed in to change notification settings - Fork 12.9k
fix(39589): await is missing after autofix convert to an async function #39649
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
...s/baselines/reference/convertToAsyncFunction/convertToAsyncFunction_PromiseAllAndThen3.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// ==ORIGINAL== | ||
|
||
function /*[#|*/f/*|]*/() { | ||
return Promise.resolve().then(() => | ||
Promise.all([fetch("https://typescriptlang.org"), fetch("https://microsoft.com"), Promise.resolve().then(function () { | ||
return fetch("https://github.com"); | ||
}).then(res => res.toString())])); | ||
} | ||
|
||
// ==ASYNC FUNCTION::Convert to async function== | ||
|
||
async function f() { | ||
await Promise.resolve(); | ||
return await Promise.all([fetch("https://typescriptlang.org"), fetch("https://microsoft.com"), Promise.resolve().then(function() { | ||
return fetch("https://github.com"); | ||
}).then(res => res.toString())]); | ||
} |
17 changes: 17 additions & 0 deletions
17
...s/baselines/reference/convertToAsyncFunction/convertToAsyncFunction_PromiseAllAndThen3.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// ==ORIGINAL== | ||
|
||
function /*[#|*/f/*|]*/() { | ||
return Promise.resolve().then(() => | ||
Promise.all([fetch("https://typescriptlang.org"), fetch("https://microsoft.com"), Promise.resolve().then(function () { | ||
return fetch("https://github.com"); | ||
}).then(res => res.toString())])); | ||
} | ||
|
||
// ==ASYNC FUNCTION::Convert to async function== | ||
|
||
async function f() { | ||
await Promise.resolve(); | ||
return await Promise.all([fetch("https://typescriptlang.org"), fetch("https://microsoft.com"), Promise.resolve().then(function() { | ||
return fetch("https://github.com"); | ||
}).then(res => res.toString())]); | ||
} |
18 changes: 18 additions & 0 deletions
18
...s/baselines/reference/convertToAsyncFunction/convertToAsyncFunction_PromiseAllAndThen4.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// ==ORIGINAL== | ||
|
||
function /*[#|*/f/*|]*/() { | ||
return Promise.resolve().then(() => | ||
Promise.all([fetch("https://typescriptlang.org"), fetch("https://microsoft.com"), Promise.resolve().then(function () { | ||
return fetch("https://github.com"); | ||
})]).then(res => res.toString())); | ||
} | ||
|
||
// ==ASYNC FUNCTION::Convert to async function== | ||
|
||
async function f() { | ||
await Promise.resolve(); | ||
const res = await Promise.all([fetch("https://typescriptlang.org"), fetch("https://microsoft.com"), Promise.resolve().then(function() { | ||
return fetch("https://github.com"); | ||
})]); | ||
return res.toString(); | ||
} |
18 changes: 18 additions & 0 deletions
18
...s/baselines/reference/convertToAsyncFunction/convertToAsyncFunction_PromiseAllAndThen4.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// ==ORIGINAL== | ||
|
||
function /*[#|*/f/*|]*/() { | ||
return Promise.resolve().then(() => | ||
Promise.all([fetch("https://typescriptlang.org"), fetch("https://microsoft.com"), Promise.resolve().then(function () { | ||
return fetch("https://github.com"); | ||
})]).then(res => res.toString())); | ||
} | ||
|
||
// ==ASYNC FUNCTION::Convert to async function== | ||
|
||
async function f() { | ||
await Promise.resolve(); | ||
const res = await Promise.all([fetch("https://typescriptlang.org"), fetch("https://microsoft.com"), Promise.resolve().then(function() { | ||
return fetch("https://github.com"); | ||
})]); | ||
return res.toString(); | ||
} |
16 changes: 16 additions & 0 deletions
16
tests/baselines/reference/convertToAsyncFunction/convertToAsyncFunction_Return1.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// ==ORIGINAL== | ||
|
||
function /*[#|*/f/*|]*/(p: Promise<unknown>) { | ||
return p.catch((error: Error) => { | ||
return Promise.reject(error); | ||
}); | ||
} | ||
// ==ASYNC FUNCTION::Convert to async function== | ||
|
||
async function f(p: Promise<unknown>) { | ||
try { | ||
return p; | ||
} catch (error) { | ||
return await Promise.reject(error); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
tests/baselines/reference/convertToAsyncFunction/convertToAsyncFunction_Return2.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// ==ORIGINAL== | ||
|
||
function /*[#|*/f/*|]*/(p: Promise<unknown>) { | ||
return p.catch((error: Error) => Promise.reject(error)); | ||
} | ||
// ==ASYNC FUNCTION::Convert to async function== | ||
|
||
async function f(p: Promise<unknown>) { | ||
try { | ||
return p; | ||
} catch (error) { | ||
return await Promise.reject(error); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
tests/baselines/reference/convertToAsyncFunction/convertToAsyncFunction_Return3.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// ==ORIGINAL== | ||
|
||
function /*[#|*/f/*|]*/(p: Promise<unknown>) { | ||
return p.catch(function (error: Error) { | ||
return Promise.reject(error); | ||
}); | ||
} | ||
// ==ASYNC FUNCTION::Convert to async function== | ||
|
||
async function f(p: Promise<unknown>) { | ||
try { | ||
return p; | ||
} catch (error) { | ||
return await Promise.reject(error); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
tests/baselines/reference/convertToAsyncFunction/convertToAsyncFunction_noArgs2.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// ==ORIGINAL== | ||
|
||
function delay(millis: number): Promise<void> { | ||
throw "no" | ||
} | ||
|
||
function /*[#|*/main2/*|]*/() { | ||
console.log("Please wait. Loading."); | ||
return delay(500) | ||
.then(() => delay(500)) | ||
.then(() => delay(500)) | ||
.then(() => delay(500)) | ||
} | ||
|
||
// ==ASYNC FUNCTION::Convert to async function== | ||
|
||
function delay(millis: number): Promise<void> { | ||
throw "no" | ||
} | ||
|
||
async function main2() { | ||
console.log("Please wait. Loading."); | ||
await delay(500); | ||
await delay(500); | ||
await delay(500); | ||
return await delay(500); | ||
} | ||
|
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.
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 could be wrong, but I believe we only need this change if the statement is inside a
try
block. Does that seem right, and do you think it would be difficult to limit the change to just those cases?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.
Do you mean for the following code the need to omit
await
in return statement and add it only if if inside try block?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.
Yeah, only if the resulting generated code is inside a
try
.return await
is usually regarded as poor style, kind of like returningPromise.resolve(something)
in athen
. But it has importantly different semantics when inside atry
block because it makes the difference between the error being handled in the function implementation vs. propagating the rejection onto the caller.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.
Example:
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.
Thanks for the clarification.
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.
@andrewbranch Should these examples also omit
await
inreturn
?TypeScript/tests/baselines/reference/convertToAsyncFunction/convertToAsyncFunction_nestedPromises.ts
Lines 10 to 12 in 083129f
or
TypeScript/tests/baselines/reference/convertToAsyncFunction/convertToAsyncFunction_callbackReturnsPromiseLastInChain.ts
Lines 9 to 11 in 083129f
I'm thinking about how to handle
await
inside/outsidetry
statement. Maybe you have some thoughts about that?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.
Hmm, that baseline pointed me to this PR: #27573
I guess this is already a known limitation of the refactor. In that case, I’m ok with your PR as-is.
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.
Well, I’m a little confused as to why #27573 didn’t already apply to some of the baselines that changed in this PR. I do think that if we start adding a lot more
return await
, people are going to file issues about it. 🤔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.
@andrewbranch Thanks for sharing this PR. I didn't know about it.
Uh oh!
There was an error while loading. Please reload this page.
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.
Basically, this PR fixes a really simple issue, it just handles
return
statements in{}
. For instance, two examples with the same logic behave differently. ExampleAbout for the test, there is a test for
{}
, however, there is no test for()
. I decided to add, to handle both cases. https://github.com/microsoft/TypeScript/pull/39649/files#diff-71c285c7a401ed150a7e8367a2d6681cR1408-R1422