Skip to content

Always await expression of promise type in return position #27573

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 1 commit into from
Oct 8, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/services/codefixes/convertToAsyncFunction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -465,19 +465,19 @@ 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);
}
return transformedStatement;
}
else {
return [createReturn(getSynthesizedDeepClone(funcBody))];
return [createReturn(possiblyAwaitedRightHandSide)];
}
}
}
Expand Down
7 changes: 7 additions & 0 deletions src/testRunner/unittests/convertToAsyncFunction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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|]() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}