2

I'm struggling to find real (or practical) use cases for Javascript generators in frontend.

I'm sure there are many cases in which they are useful (trees and so on). The thing is I don't usually implement trees in day to day live.

So far, I haven't found any case in which the generator couldn't be easily replaced by a familiar while or for structure (full supported by all browsers and understood by all devs).

I'd like to know if any of you have found a practical case in frontend in which it was really useful and worth using or if you think that's nothing more than a fancy feature that will become deprecated in the long run.

acontell
  • 6,792
  • 1
  • 19
  • 32
  • Yes, JS is a turing-complete language, so you can replace *everything* (computable) with an existing structure. Generators just make some of them a lot simpler. – Bergi Jan 03 '17 at 14:56
  • It depends on what you understand by simple – acontell Jan 03 '17 at 14:57
  • Btw, i understand how generators work, im asking for real examples – acontell Jan 03 '17 at 14:58
  • See the duplicate for a real example that is much simpler with a generator function. – Bergi Jan 03 '17 at 17:55
  • I'm looking for examples of more day to day use cases. I guess it'll have its applications in data structures and so on, but I fail to see its uses in enterprise frontend applications (usually the ones that pay our rents). That's the reason why I don't think this question is a duplicate and why that example is not enough. But thanks anyway – acontell Jan 03 '17 at 18:36
  • Well I don't know what kind of code you write day to day, it might easily be one that does not benefit from generators. But even front end applications do involve data structures, for example the DOM is one large tree an you could traverse it using a generator (that might be simpler than the builtin [`NodeIterator`](https://developer.mozilla.org/en-US/docs/Web/API/NodeIterator). Also there are advanced usages of generators for event systems like CSP that might be helpful on the frontend. – Bergi Jan 03 '17 at 19:32

0 Answers0