Skip to content

Compiler suggestion: explicitly await #10381

Closed

Description

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.

Activity

  1. yortus commented on Aug 17, 2016

    @yortus
    Contributor

    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.

  2. nippur72 commented on Aug 17, 2016

    @nippur72

    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 ?

  3. RyanCavanaugh commented on Aug 17, 2016

    @RyanCavanaugh
    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.

  4. nippur72 commented on Aug 17, 2016

    @nippur72

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

  5. Roam-Cooper commented on Aug 18, 2016

    @Roam-Cooper
    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.

  6. locked and limited conversation to collaborators on Jun 19, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Out of ScopeThis idea sits outside of the TypeScript language design constraintsSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions