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.

Proposed rule: no-await-in-loop

See original GitHub issue

When does this rule warn?

This rule warns when a developer uses await inside of a loop. Doing so means that each loop iteration will run in serial, when often the desired behavior is to dispatch a bunch of asynchronous operations and then await on the result as a whole, using Promise.all.

Occasionally, awaiting inside a loop actually is desirable. In cases like those,

Here is an example:

async foo() {
  for (const bar of baz) {
    await(bar);
  }
}

Here is my proposed warning:

‘Avoid using await inside a loop. Consider refactoring to use Promise.all. If you are sure you want to do this, add // eslint-disable-line no-await-in-loop at the end of this line.’

Is this rule preventing an error or is it stylistic?

This rule prevents an error. Needlessly awaiting inside of a loop can have serious performance implications.

Why is this rule a candidate for inclusion instead of creating a custom rule?

Using await inside of a loop is a bad pattern no matter what codebase you are operating in. There are occasional places where it is desirable but it is almost always a sign that you have done something wrong.

My only concern is that this rule will not operate with the eslint frontend – to my knowledge, it has not implemented async/await parsing yet. Therefore it could be confusing to have a rule built-in to eslint that requires its users to use babel-eslint.

Are you willing to create the rule yourself?

Yes. I have already written the first version.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:6
  • Comments:19 (18 by maintainers)

github_iconTop GitHub Comments

2reactions
kaicataldocommented, Nov 4, 2016

@nmote This issue is now accepted - let us know if you have any issues and thanks for being willing to work on this!

2reactions
kaicataldocommented, Nov 4, 2016

Sorry for holding this up. I’m still not a huge fan of relying on the built in eslint-disable comments (rather than a rule-specific thing), but given that’s my only concern, I support this rule. We can always add a rule-specific way to disable if it feels necessary in the future.

Read more comments on GitHub >

github_iconTop Results From Across the Web

no-await-in-loop - ESLint - Pluggable JavaScript Linter
A pluggable and configurable linter tool for identifying and reporting on patterns in JavaScript. Maintain your code quality with ease.
Read more >
Unexpected `await` inside a loop. (no-await-in-loop)
But I tried with recursive function call to iterate array. const putDelay = (ms) => new Promise((resolve) => ...
Read more >
Why and How to Avoid Await in a For-Loop | by Chris Fields II
This is due to tagging the function passed into the forEach() as async which immediately returns a Promise and proceeds to the next...
Read more >
no-await-in-loop - ESLint Config
This rule disallows the use of await within loop bodies. What ESLint should do when it catches the rule break. Show a warning ......
Read more >
[AskJS] how do you feel about no-await-in-loop eslint rule?
Or, loops may be used to retry asynchronous operations that were unsuccessful. Or, loops may be used to prevent your code from sending...
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