Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use for loop for array provide errror

I use the following code that works as expected, while using a ESLINT i got error

ESLint: iterators/generators require regenerator-runtime, which is too heavyweight for this guide to allow them. Separately, loops should be avoided in favor of array iterations. (no-restricted-syntax)

This is the code

for (const subscription of resp.data.subscriptions) {
  if (subscription.url) {
    return subscription.url;
  }
}

The code is simply

  • get an array of data from other function
  • loop on each array item
  • when the first array instance have url take it and return

Is there a way to write it better to avoid the eslint issue ?

like image 359
Beno Odr Avatar asked Sep 02 '26 22:09

Beno Odr


1 Answers

There is a debate about for...of usage here and its eventual restriction

for(let i = 0; i < array.length; i ++) { ... } is antiquated syntax, and while I know everyone understands what it means, we should be leaving it behind.

array.map has functional connotations and we shouldn't be producing side effects in the closure.

array.forEach is an option, but I personally don't like it for this sort of imperative work.

So I think the ForOfStatement should be removed from the restricted syntax for the above reasons - anyone with any conflicting viewpoints? Do we know what the original justification is?

for..of is more expensive than forEach, check this out

I have no opinions, you could just remove the eslint rule

like image 65
Maxime Helen Avatar answered Sep 04 '26 11:09

Maxime Helen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!