-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
Proposal: Code Fix -- Convert Promise Handlers to Async and Await #25082
Comments
I think that the promises should be Also, the output in the |
I wanted to make this as VSCode extension for quite some time. Landing this in typescript of course makes much more sense, because this way any IDE integrating the language server can offer this refactor very easily. Awesome stuff-is there a code somewhere which does the refactor itself @elizabethdinella ? |
How will this deal with things that are like Promises but not quite?
Edit: oh, and is it out of scope to suggest this fix? const sample = async () => {
if (condition) {
// Just return;
return Promise.resolve();
}
}; |
So for one of your async function foo():Promise<void> {
let result;
try {
result = await fetch('https://microsoft.com');
await console.log(result);
}
catch (err) {
await console.log(err);
}
} Ideally async function foo():Promise<void> {
- let result;
try {
- result = await fetch('https://microsoft.com');
+ let result = await fetch('https://microsoft.com');
await console.log(result);
}
catch (err) {
await console.log(err);
}
} Can you give specific labels to examples? It'd make it easier to reference 😃 |
For similar reasons, this is also problematic: async function foo(): Promise<void> {
let result;
try {
result = await fetch('https://microsoft.com');
}
catch (rej) {
return console.log("error:", rej);
}
return result.ok;
} Here, you will actually introduce a failure if a rejection occurred, since |
@DanielRosenwasser How would |
This proposal allows the Typescript language service to automatically refactor code that uses Promise methods such as
.then()
or.catch()
to instead use theasync
andawait
keywords.Async/await offers many advantages over promise methods including cleaner syntax, error handling, debugging, and readability. The benefits of the synchronous style code provided by async/await are largely recognized by the Javascript community. However, there is still a substantial amount of asynchronous code written using Promises. This proposal will provide a quick and simple transition to using the async/await keywords.
Simple Examples
Input:
Output:
onRejected handlers:
Input:
Output:
Note that this conversion does not preserve semantics. In order to create clean, readable code, the semantics of some code is slightly altered.
Catch handlers:
Input:
Output:
More Examples - multiple handlers
Multiple .then() calls:
In situations where variable names intersect, a new variable name will be chosen for intermediate variables. For example:
Input:
Output:
Multiple .catch() calls
Input:
Output:
More Examples - Promise.all() and Promise.race()
In order to preserve semantics, code that uses
Promise.all()
cannot be entirely refactored.Input
Output:
Similarly, code that uses 'Promise.race()' is partially refactored:
Input
Output:
More Examples - Returning variables of Promise type
In order to preserve semantics, a refactoring will only be offered for functions that directly return a promise with callbacks.
For example, a refactoring will not be offered for the following:
However, a refactoring will be offered for the following function as it can be converted to use async and await while preserving semantics:
Support for
.finally()
will be coming laterThe text was updated successfully, but these errors were encountered: