-
Notifications
You must be signed in to change notification settings - Fork 13.2k
Description
First of all, thanks a lot for this refactoring, I think it's really useful and will push the adoption of async-await syntax 👍
This is a bug report related to this Twitter conversation: The converted code does not await for Promise.reject, thus the runtime does not throw but returns Promise.reject(3), which does not trigger the catch-clause but makes the function call return a rejected Promise.
I'm not 100% sure what the best conversion would be, but I assume it would be to always await for functions that could possibly return a Promise or expressions that are a Promise.
TypeScript Version: Tested against https://github.com/Microsoft/TypeScript/tree/v3.1.1 and 4ed85b7
Failing testcases:
- On top of
master: https://github.com/rradczewski/TypeScript/tree/bug-return-needs-await (master...rradczewski:bug-return-needs-await) - On top of
v3.1.1: https://github.com/rradczewski/TypeScript/tree/bug-v3.1.1-return-needs-await (v3.1.1...rradczewski:bug-v3.1.1-return-needs-await)
These assume that the correct behavior is to add await to a returned expression that yields a Promise.
Search Terms:
- async
- convertToAsync
Code
function testcase() {
return Promise.resolve(1)
.then(x => Promise.reject(2))
.catch(err => console.log(err));
}Running testcase() code will log 2 and the returned Promise will resolve to undefined.
Expected behavior:
async function testcase() {
try {
const x = await Promise.resolve(1);
return await Promise.reject(2);
}
catch (err) {
return console.log(err);
}
}A call to testcase() would print 2 and the returned Promise would resolve to undefined as above.
Actual behavior:
async function testcase() {
try {
const x = await Promise.resolve(1);
return Promise.reject(2);
}
catch (err) {
return console.log(err);
}
}A call to testcase() this won't print 2 but node will complain about an unhandled rejected promise.
Related Issues:
- Proposal: Code Fix -- Convert Promise Handlers to Async and Await #25082 - The proposal, while mentioning "changed semantics", gives two conflicting examples:
- Catch handlers has
await console.log(...) - Multiple .catch() calls does not add
awaittoconsole.log
- Catch handlers has
- Possibly in async code fix, handle promise handler callbacks that return a promise #27112 and Async code fix issues concerning underscores and nested promises #27156