6

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 ?

0

2 Answers 2

12

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

Sign up to request clarification or add additional context in comments.

3 Comments

I am particularly against bulls**t like "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." Everyone understands it. It is BY FAR the most performant code in the bunch, but we should "leave it behind". We don't have a compiler to mitigate the effects of iterators on performance, so throwing away your most performing solution is simply dumb.
I agree, developers affects sugar syntax way too much sometimes and want to be cool by using latest. But forEach could improve readability so, as I said, I have no opinions
forEach isn't always a workable substitute, though, since break isn't valid in a forEach loop. If you need to bail when something has happened once (e.g., in the code I'm doing now, when the first shape has a true hit test, I don't want to look at any more), then we're back to the same choices again.
1

You can use a .filter to solve this:

var arrObjWithURL = resp.data.subscriptions.filter(function(item) {
    return item.url;
});

if (arrObjWithURL.length > 0) {
    return arrObjWithURL[0];
}

You can also use .find:

return resp.data.subscriptions.find(function(item) {
    return item.url;
});

... or this to continue if there is no URL found:

const objWithUrl = resp.data.subscriptions.find(function(item) {
    return item.url;
});

if (objWithUrl !== undefined) {
    return objWithUrl;
}

demo on jsfiddle.net

4 Comments

Maybe find is a better option for this problem
Thanks, assume I've another code after the find function it's not reacble, how can overcome this? I mean if find find url return, otherwise proceed to the next code in the function ....
@BenoOdr updated answer. You can check the return value of find for undefined.
Thanks, one last question, I want to learn :) the find code is better on this case then the for in nodejs ? I mean looping on array, which have better performance ? im not an expert...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.