New Rule: no-async-in-foreach #16330
Labels
archived due to age
This issue has been archived; please open a new issue for any further discussion
feature
This change adds a new feature to ESLint
rule
Relates to ESLint's core rules
Projects
Rule details
Warn about using
async
within Array.forEachRelated ECMAScript feature
ECMAScript 2017 - await/async
What type of rule is this?
Warns about a potential problem
Example code
Why should this rule be in the core instead of a plugin?
It is dangerous to use async inside
forEach
because the promises side-effects will not be evaluated before theforEach
completes.In environments such as AWS Lambda, this is even worse, because the execution environment freezes pending promises on the event loop and may resume the promises if the execution environment is re-used (see https://levelup.gitconnected.com/avoiding-the-pitfalls-of-async-node-js-functions-in-aws-lambda-941220582e7a). This will result in the side-effect of the async
forEach
either:Given the danger of this oversight, I think this should be a core rule to protect junior and inexperienced developers from this easy mistake.
Participation
Additional comments
I know this issue is not related to new ECMAScript features, but given the severity of the issue, I really think it should be added to core.
This topic was previously brought up in #12576 but they proposed solving it by extending
no-await-in-loop
which has a different purpose - it is designed to optimise execution by moving sequential to parallel execution. This issue is different as this is far worse than suboptimal execution so therefore I think this should be a separate rule.The text was updated successfully, but these errors were encountered: