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

Conversation

RyanCavanaugh
Copy link
Member

Fixes #13376

@cevek
Copy link

cevek commented Apr 24, 2017

@RyanCavanaugh I think it would very useful also to check calls without await in control flow condition statements

Because to catch errors like this I need tslint with slow "strict-boolean-expressions" rule

async function bar(){return false}

async function foo() {
  if (bar()) {}
  while (bar()) {}  
  do {} while (bar())
  var x = bar() ? 1 : 2;
  for (var x = 1; i++; bar()){}
  switch (bar()) {case bar(): break;}
}

@kpreisser
Copy link
Contributor

kpreisser commented Jun 12, 2017

Hi,
is there an ETA when this PR will be merged to master (e.g. after a 2.4 branch was created)?
Currently I'm merging this branch for a custom TS build and every time I have to fix the merge conflict in diagnosticMessages.json. 😉

Thanks!

@finnigantime
Copy link

Would love to have this! Currently relying on tslint rule no-floating-promises but having this guard in TypeScript itself seems preferable.

@leebenson
Copy link

This would be especially useful for VSCode users, where no-floating-promises doesn't work out-the-box with the TSLint plugin. Having code look fine but then throw when hitting tslint via the CLI or VSCode task is annoying.

@mhegazy
Copy link
Contributor

mhegazy commented Nov 7, 2017

in light of the discussion about errors vs warnings we should instead move this to tslint.

@leebenson
Copy link

It already is in tslint. This is problematic enough to be in TS proper. The tslint plugin in VS code doesn’t catch it and those without a separate linting step in their build pipeline could be shipping very broken code. I hit a MAJOR security issue with not awaiting a Promise— no way should this be downgraded to a warning.

@conordickinson
Copy link

The TSLint rule also requires type info to be on, which we do not do because it is so much slower (and causes tslint to run out of memory with our repo last I tried it).

@leebenson
Copy link

leebenson commented Nov 8, 2017

Merging this would prevent things like this:

try {
  await someImportantSecurityCheck(); // <-- ditching `await` is a MAJOR security flaw
  runSomePrivilegedFunction(); <-- drop `await` above, and this runs... yikes
} catch (e) {
  // deal with unauthorised...
}

// ...

Which incidentally, is exactly the issue I ran into.

Not awaiting promises can cause major, not easily caught security headaches, race conditions and non-deterministic code. Not to mention unhandled Promise rejections are deprecated, and will eventually terminated with a non-zero error code.

Again - this is not linting. It's a significant flaw in an otherwise decent type system.

Promises should be dealt with based on type, period. If we don't care about how or when they run, we stick void in front of them. But it should absolutely be explicit.

@RyanCavanaugh
Copy link
Member Author

Will pick this up later

@ajafff ajafff mentioned this pull request Feb 27, 2018
2 tasks
@microsoft microsoft locked and limited conversation to collaborators Jun 14, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants