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

Compiler suggestion: explicitly await #10381

Closed
Roam-Cooper opened this issue Aug 17, 2016 · 5 comments
Closed

Compiler suggestion: explicitly await #10381

Roam-Cooper opened this issue Aug 17, 2016 · 5 comments
Labels
Out of Scope This idea sits outside of the TypeScript language design constraints Suggestion An idea for TypeScript

Comments

@Roam-Cooper
Copy link

I'm not incredibly familiar with the compiler's internals, so if someone wants to rewrite this suggestion as a proposal, go ahead!

I've noticed it is quite easy for a developer to call an async function/method without awaiting on it, so it would be nice to have a compiler option similar to noimplicitany;noimplicitreturns; etc, where a developer has to explicitly declare that they wish to call an async function/method without awaiting on it.

This would help keep developers aware that the function/method is asynchronous so that they don't accidentally call it without awaiting on it.

@yortus
Copy link
Contributor

yortus commented Aug 17, 2016

There are many valid things to do with an async function other than awaiting it. It's just a function that returns a promise, after all. So you may want to explicitly assign that promise to a variable or push it into an array and then use Promise.all, etc.

Perhaps rather than looking for an explicit await, it would be better to just flag cases where the return value of the async function call has not been used at all. Such calls often do indicate a potential error, where the programmer expects the function call to complete before the next statement executes.

@nippur72
Copy link

With type annotations the error is easily spotted, e.g. the compiler will warn if you are trying to use a Promise<number> instead of a number. But still it can be useful if the compiler emits a dedicated warning/error. Perhaps a rule for tslint ?

@RyanCavanaugh RyanCavanaugh added Suggestion An idea for TypeScript Out of Scope This idea sits outside of the TypeScript language design constraints labels Aug 17, 2016
@RyanCavanaugh
Copy link
Member

The problem is that, as noted, there are an infinite number of valid ways to "correctly" handle a Promise (and people will have varying definitions of "correct", including those who want to conditionally not await something ever!). If you want to enforce in your project that you only handle them in a finite set of allowed ways, that's a good candidate for a TSLint rule.

@nippur72
Copy link

nippur72 commented Aug 17, 2016

looks like someone is already trying to write it: palantir/tslint#892

@Roam-Cooper
Copy link
Author

Oh, yes. Adding it to linting is definitely a good idea.

I just thought it might be able to be implemented as an "unused variable/value" type warning. Linting works for us, though.

@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Out of Scope This idea sits outside of the TypeScript language design constraints Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

4 participants