question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[no-await-in-loop] should check for `await` keyword inside `forEach` loop

See original GitHub issue

What rule do you want to change?

no-await-in-loop

Does this change cause the rule to produce more or fewer warnings? More.

How will the change be implemented? (New option, new default behavior, etc.)? It supposed to include new checks.

Please provide some example code that this change will affect:

function delay() {
  return new Promise(resolve => setTimeout(resolve, 300));
}

async function delayedLog(item) {
  await delay();
  console.log(item);
}
async function processArray(array) {
  array.forEach(async (item) => {
    await delayedLog(item);
  })
  console.log('Done!');
}

processArray([1, 2, 3]);

Output:

Done!
1
2
3

Link to read https://lavrton.com/javascript-loops-how-to-handle-async-await-6252dd3c795/

What does the rule currently do for this code? It allows using await inside forEach loop.

What will the rule do after it’s changed? It will warn “Unexpected await inside a loop.”. Probably it makes sense to warn a developer kind used of the loop. (for/of, forEach, etc.).

Are you willing to submit a pull request to implement this change? Yes, it’s a good chance to start contributing eslint.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
dimaborycommented, Nov 20, 2019

@kaicataldo I’ve been thinking how to deal with such kind of buggy code, but TBH I have no idea right now how to enforce this rule 🤷‍♂️ … and yes, you’re definitely right that it has little to do with no-await-in-loop rule.

0reactions
eslint-deprecated[bot]commented, Dec 21, 2019

Unfortunately, it looks like there wasn’t enough interest from the team or community to implement this change. While we wish we’d be able to accommodate everyone’s requests, we do need to prioritize. We’ve found that issues failing to reach accepted status after 21 days tend to never be accepted, and as such, we close those issues. This doesn’t mean the idea isn’t interesting or useful, just that it’s not something the team can commit to.

Thanks for contributing to ESLint and we appreciate your understanding.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using async/await with a forEach loop - Stack Overflow
I'd like to address the difference between using forEach and for loop when it comes to async and await. how forEach works. Let's...
Read more >
The Simplest Guide to Using Async/Await with forEach() in ...
Using async/await with forEach() does not need to be a nightmare! Here are 4 solutions to your problem.
Read more >
How to use async/await inside loops in JavaScript
In this article, we will discuss the best approaches to combine async/await and iterative logic. There will be a time when you would...
Read more >
Iterating Asynchronously: How to use async & await ... - Medium
In this post, we will look at how we go about iterating using a foreach loop asynchronously. Now you may be thinking why...
Read more >
JavaScript: async/await with forEach() | by Sebastien Chopin
log(num) are not displayed into the console. Let's re-create forEach() to understand what's happening: Array.prototype.forEach = function (callback) { // this ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found