1

Here is the situation:

I am calling the following function which compute a large set of Elastic Data (the buckets variable is the set of documents)

    await this.computeAllData(start, range, wait, buckets, accumulator);

Here is the code inside the previous function:

  async computeAllData(start, range, wait, buckets, accumulator) {
    let nextAfter = false;
    do { 
      nextAfter = await this.computeOne(
        start,
        range,
        wait,
        buckets,
        accumulator,
        nextAfter
      );
    } while (nextAfter);
  }

The problem is : I should not use "await" inside a loop (ESLint rules). But I can find a way to keep the behavior of the do...while()(stop the loop when data were digest) with the async/await approach.

I try with yield/generator approach but it's a failure, too complex for me :(

Please any idea ?

Arnaud Dev
  • 53
  • 5
  • 1
    The question isn't how to fix it, but rather why such a rule has been implemented. You see the rule exists in place so that the application makes better use of the parallization possibilities. To not wait for every single iteration before starting the next one. Depending on the example rule may not be a proper solution. In your case the result of one iteration is required for the next one making this a totally valid appraoch. – Krzysztof Krzeszewski Sep 10 '21 at 09:29
  • 1
    From [ESLint: Disallow await inside of loops (no-await-in-loop): When Not To Use It](https://eslint.org/docs/rules/no-await-in-loop#when-not-to-use-it) `[…]In many cases the iterations of a loop are not actually independent of each-other. For example, the output of one iteration might be used as the input to another. […] In such cases it makes sense to use await within a loop and it is recommended to disable the rule via a standard ESLint disable comment.` – t.niese Sep 10 '21 at 09:52

1 Answers1

2

No, that ESLint rule is stupid. It should be a warning to suggest that maybe you could run the asynchronous actions concurrently. But no, you absolutely cannot in your case, you want your processing to be sequential. Ignore the rule or disable it altogether.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375