mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-02-15 03:23:08 -06:00
Always await expression of promise type in return position
This commit is contained in:
parent
e1d346ea53
commit
2ebd986d99
@ -465,11 +465,11 @@ namespace ts.codefix {
|
||||
return innerCbBody;
|
||||
}
|
||||
|
||||
const type = transformer.checker.getTypeAtLocation(func);
|
||||
const returnType = getLastCallSignature(type, transformer.checker)!.getReturnType();
|
||||
const rightHandSide = getSynthesizedDeepClone(funcBody);
|
||||
const possiblyAwaitedRightHandSide = !!transformer.checker.getPromisedTypeOfPromise(returnType) ? createAwait(rightHandSide) : rightHandSide;
|
||||
if (!shouldReturn) {
|
||||
const type = transformer.checker.getTypeAtLocation(func);
|
||||
const returnType = getLastCallSignature(type, transformer.checker)!.getReturnType();
|
||||
const rightHandSide = getSynthesizedDeepClone(funcBody);
|
||||
const possiblyAwaitedRightHandSide = !!transformer.checker.getPromisedTypeOfPromise(returnType) ? createAwait(rightHandSide) : rightHandSide;
|
||||
const transformedStatement = createTransformedStatement(prevArgName, possiblyAwaitedRightHandSide, transformer);
|
||||
if (prevArgName) {
|
||||
prevArgName.types.push(returnType);
|
||||
@ -477,7 +477,7 @@ namespace ts.codefix {
|
||||
return transformedStatement;
|
||||
}
|
||||
else {
|
||||
return [createReturn(getSynthesizedDeepClone(funcBody))];
|
||||
return [createReturn(possiblyAwaitedRightHandSide)];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -1236,6 +1236,13 @@ function [#|f|]() {
|
||||
}
|
||||
`);
|
||||
|
||||
_testConvertToAsyncFunction("convertToAsyncFunction_callbackReturnsRejectedPromiseInTryBlock", `
|
||||
function [#|f|]() {
|
||||
return Promise.resolve(1)
|
||||
.then(x => Promise.reject(x))
|
||||
.catch(err => console.log(err));
|
||||
}
|
||||
`);
|
||||
|
||||
_testConvertToAsyncFunction("convertToAsyncFunction_nestedPromises", `
|
||||
function [#|f|]() {
|
||||
|
||||
@ -8,5 +8,5 @@ function /*[#|*/f/*|]*/() {
|
||||
|
||||
async function f() {
|
||||
const s = await fetch('https://typescriptlang.org');
|
||||
return Promise.resolve(s.statusText.length);
|
||||
return await Promise.resolve(s.statusText.length);
|
||||
}
|
||||
|
||||
@ -8,5 +8,5 @@ function /*[#|*/f/*|]*/() {
|
||||
|
||||
async function f() {
|
||||
const s = await fetch('https://typescriptlang.org');
|
||||
return Promise.resolve(s.statusText.length);
|
||||
return await Promise.resolve(s.statusText.length);
|
||||
}
|
||||
|
||||
@ -0,0 +1,19 @@
|
||||
// ==ORIGINAL==
|
||||
|
||||
function /*[#|*/f/*|]*/() {
|
||||
return Promise.resolve(1)
|
||||
.then(x => Promise.reject(x))
|
||||
.catch(err => console.log(err));
|
||||
}
|
||||
|
||||
// ==ASYNC FUNCTION::Convert to async function==
|
||||
|
||||
async function f() {
|
||||
try {
|
||||
const x = await Promise.resolve(1);
|
||||
return await Promise.reject(x);
|
||||
}
|
||||
catch (err) {
|
||||
return console.log(err);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,19 @@
|
||||
// ==ORIGINAL==
|
||||
|
||||
function /*[#|*/f/*|]*/() {
|
||||
return Promise.resolve(1)
|
||||
.then(x => Promise.reject(x))
|
||||
.catch(err => console.log(err));
|
||||
}
|
||||
|
||||
// ==ASYNC FUNCTION::Convert to async function==
|
||||
|
||||
async function f() {
|
||||
try {
|
||||
const x = await Promise.resolve(1);
|
||||
return await Promise.reject(x);
|
||||
}
|
||||
catch (err) {
|
||||
return console.log(err);
|
||||
}
|
||||
}
|
||||
@ -9,5 +9,5 @@ function /*[#|*/f/*|]*/() {
|
||||
async function f() {
|
||||
const x = await fetch('https://typescriptlang.org');
|
||||
const y = await Promise.resolve(3);
|
||||
return Promise.resolve(x.statusText.length + y);
|
||||
return await Promise.resolve(x.statusText.length + y);
|
||||
}
|
||||
|
||||
@ -9,5 +9,5 @@ function /*[#|*/f/*|]*/() {
|
||||
async function f() {
|
||||
const x = await fetch('https://typescriptlang.org');
|
||||
const y = await Promise.resolve(3);
|
||||
return Promise.resolve(x.statusText.length + y);
|
||||
return await Promise.resolve(x.statusText.length + y);
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user