Skip to content

Warn on forEach(async  #28595

Closed
Closed

Description

Search Terms

  • forEach
  • forEach(async

Suggestion

A simple warning when someone uses forEach(async. It's such an easy stumbling block for those entering the async/await world. A user never wants to write that code intentionally

Use Cases

I didn't know that forEach(async ... simply doesn't work (even though linting passes, and similar things like .map(async and relatives are fine.

Examples

Wherever the parser encounters forEach(async it marks it as an error.

Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

Activity

  1. weswigham commented on Nov 19, 2018

    @weswigham
    Member

    I think this is probably the domain of a new lint rule - generally speaking, any non-undefined returning function used with Array.prototype.forEach is probably an error, since the return type is unused, but there's technically nothing wrong with it - forEach'ing over a function with a real-valued return type (eg, Promise) for sideeffects is potentially valid.

  2. added
    ExternalRelates to another program, environment, or user action which we cannot control.
    on Nov 19, 2018
  3. ajafff commented on Nov 23, 2018

    @ajafff
    Contributor

    It turns out it's not too difficult to implement. So there you have it: https://github.com/fimbullinter/wotan/blob/master/packages/mimir/docs/async-function-assignability.md
    This rule does exactly what is asked for in the opening post. It covers a lot more cases and should detect almost all assignments of async functions to void-returning functions.

    Have a look at the documentation of the linter for a quick start guide: https://github.com/fimbullinter/wotan/blob/master/packages/wotan/README.md

  4. chaffeqa commented on Nov 23, 2018

    @chaffeqa
    Author

    that is Klaus Meinhardt (@ajafff) would love to get that into our codebase... so you know, i can stop making terrible bugs for myself 😆

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

    ExternalRelates to another program, environment, or user action which we cannot control.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions