Skip to content
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

Disallow calls to async functions from async functions without using 'await' #15195

Closed
wants to merge 2 commits into from
Closed
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
13 changes: 12 additions & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19388,7 +19388,18 @@ namespace ts {
// Grammar checking
checkGrammarStatementInAmbientContext(node);

checkExpression(node.expression);
const type = checkExpression(node.expression);

// Check for missing 'await' keyword in async functions
if (type &&
type.symbol === getGlobalPromiseType(/*reportErrors*/ false).symbol &&
node.expression.kind === ts.SyntaxKind.CallExpression) {
// Calls to async functions without await inside async functions are disallowed
const container = getContainingFunction(node);
if (container && isAsyncFunction(container)) {
error(node.expression, Diagnostics.Return_value_of_async_function_call_was_discarded_Did_you_mean_to_await_its_result);
}
}
}

function checkIfStatement(node: IfStatement) {
Expand Down
4 changes: 4 additions & 0 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -2107,6 +2107,10 @@
"category": "Error",
"code": 2709
},
"Return value of async function call was discarded. Did you mean to 'await' its result?": {
"category": "Error",
"code": 2710
},

"Import declaration '{0}' is using private name '{1}'.": {
"category": "Error",
Expand Down
36 changes: 36 additions & 0 deletions tests/baselines/reference/missedAwait.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
tests/cases/compiler/missedAwait.ts(18,5): error TS2710: Return value of async function call was discarded. Did you mean to 'await' its result?
tests/cases/compiler/missedAwait.ts(19,5): error TS2710: Return value of async function call was discarded. Did you mean to 'await' its result?


==== tests/cases/compiler/missedAwait.ts (2 errors) ====
async function isAsync() {
return 10;
}

class SomeClass {
async foo() {
return "ok";
}
}

var x = new SomeClass();
function notAsync() {
isAsync(); // OK
x.foo(); // OK
}

async function alsoAsync() {
isAsync(); // No
~~~~~~~~~
!!! error TS2710: Return value of async function call was discarded. Did you mean to 'await' its result?
x.foo(); // No
~~~~~~~
!!! error TS2710: Return value of async function call was discarded. Did you mean to 'await' its result?
void isAsync(); // OK
void x.foo(); // OK

var j = x.foo(); // OK
j = x.foo(); // OK
await isAsync(); // OK
await x.foo(); // OK
}
67 changes: 67 additions & 0 deletions tests/baselines/reference/missedAwait.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
//// [missedAwait.ts]
async function isAsync() {
return 10;
}

class SomeClass {
async foo() {
return "ok";
}
}

var x = new SomeClass();
function notAsync() {
isAsync(); // OK
x.foo(); // OK
}

async function alsoAsync() {
isAsync(); // No
x.foo(); // No
void isAsync(); // OK
void x.foo(); // OK

var j = x.foo(); // OK
j = x.foo(); // OK
await isAsync(); // OK
await x.foo(); // OK
}

//// [missedAwait.js]
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
function isAsync() {
return __awaiter(this, void 0, void 0, function* () {
return 10;
});
}
class SomeClass {
foo() {
return __awaiter(this, void 0, void 0, function* () {
return "ok";
});
}
}
var x = new SomeClass();
function notAsync() {
isAsync(); // OK
x.foo(); // OK
}
function alsoAsync() {
return __awaiter(this, void 0, void 0, function* () {
isAsync(); // No
x.foo(); // No
void isAsync(); // OK
void x.foo(); // OK
var j = x.foo(); // OK
j = x.foo(); // OK
yield isAsync(); // OK
yield x.foo(); // OK
});
}
29 changes: 29 additions & 0 deletions tests/cases/compiler/missedAwait.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// @target: es6

async function isAsync() {
return 10;
}

class SomeClass {
async foo() {
return "ok";
}
}

var x = new SomeClass();
function notAsync() {
isAsync(); // OK
x.foo(); // OK
}

async function alsoAsync() {
isAsync(); // No
x.foo(); // No
void isAsync(); // OK
void x.foo(); // OK

var j = x.foo(); // OK
j = x.foo(); // OK
await isAsync(); // OK
await x.foo(); // OK
}