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 rule occurs when an await is inside a do while loop

See original GitHub issue

Hi I am new in using ESLint. I’ve noticed that ESLint does have a rule no-await-in-loop. I’m kind of struggling to implement my logic due to the restriction of await inside a loop. the code below will get Promise {<pending>} when i’m not appending the await. but works perfectly fine if it has await in it.

async searchElement(locator) {
    let lookForElement;
    let limitCounter;

    do {
      lookForElement = await this.client.isVisible(locator); //this line will have the no-await-in-loop
      limitCounter = await this.client.isVisible(locator); //this line will have the no-await-in-loop

      if (lookForElement === false) {
        await this.swipe(81, 700, 81, 682); //this line will have the no-await-in-loop
        await this.pauseForInSeconds(0.7); //this line will have the no-await-in-loop
      } else {
        await this.clickOn(locator); //this line will have the no-await-in-loop
        break;
      }
    } while (limitCounter === false);

I believe that this code is valid but ESLint detecting all await should not be inside a loop. I would like to understand better on why there is such rule (I am very sorry I am very new to javascript and eslint). And since there is this rule. what are my other options to implement this logic since I really need to put it on a do while loop. Please let me know if you need more information. thanks alot.

P.S. the temporary solution i’m doing now is to disable the rule by: // eslint-disable-next-line no-await-in-loop

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
ljharbcommented, Mar 19, 2019

@vamshi9 This really isn’t the place to discuss it, but the number of tasks you have isn’t changed by using a loop or by using iterative means. Ping me on irc or in gitter with your code and I’ll be happy to show you how to achieve it without loops.

1reaction
jtiscionecommented, Sep 24, 2018

This rule is assuming Promise.all() is always an option, which it usually isn’t. I was surprised by this rule. Nobody’s going to write a for loop with a bunch of async calls unless they need to run sequentially… right?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Unexpected `await` inside a loop. (no-await-in-loop)
Performing await inside loops can be avoided once iterations doesn't have dependency in most cases, that's why eslint is warning it here.
Read more >
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 >
Why and How to Avoid Await in a For-Loop | by Chris Fields II
In this case getUser is as asynchronous function that is hardcoded to return a user in 3 seconds.
Read more >
[AskJS] how do you feel about no-await-in-loop eslint rule?
In such cases it makes sense to use await within a loop and it is recommended to disable the rule via a standard...
Read more >
JavaScript async and await in loops | Zell Liew
When you use await , you expect JavaScript to pause execution until the awaited promise gets resolved. This means await s in a...
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