Skip to content

Conversation

@Hweinstock
Copy link
Contributor

@Hweinstock Hweinstock commented Dec 17, 2024

Problem

usage of an async function inside a forEach, which is almost always not desired. forEach will not await the results leading to race conditions and inconsistent orderings.

Solution

  • add lint rule
  • migrate existing cases

  • Treat all work as PUBLIC. Private feature/x branches will not be squash-merged at release time.
  • Your code changes must meet the guidelines in CONTRIBUTING.md.

License: I confirm that my contribution is made under the terms of the Apache 2.0 license.

@github-actions

This comment was marked as off-topic.

@Hweinstock Hweinstock changed the title refactor(cwl): avoid async method inside forEach refactor(general): avoid async method inside forEach Dec 17, 2024
{ code: 'list.forEach(async (a) => await Promise.resolve(a * a))', errors: [errMsg] },
{ code: 'list.forEach(async (a: any) => console.log(x))', errors: [errMsg] },
{ code: 'list.forEach((a) => a.forEach(async (b) => a * b))', errors: [errMsg] },
{ code: 'list.forEach(async function () {})', errors: [errMsg] },
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what about

const fn = async (item) => {}
list.forEach(fn)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See recent commits, also added a few more edge cases.

@Hweinstock Hweinstock changed the title refactor(general): avoid async method inside forEach refactor(general): avoid async method inside forEach (WIP) Dec 18, 2024
'aws-toolkits/no-console-log': 'error',
'aws-toolkits/no-json-stringify-in-log': 'error',
'aws-toolkits/no-printf-mismatch': 'error',
'aws-toolkits/no-async-in-foreach': 'error',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we just ban forEach? it is kind of pointless.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea, I think banning it out right has a few advantages. Additionally, determining if a function is async is definitely non-trivial. This work is moved to #6281

@Hweinstock Hweinstock closed this Dec 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants