re: Use-Cases For JavaScript Generators VIEW POST

re: Here is the first example (looping arrays using generators to pause and resume): function* makeGenLoop(arr) { for (const item of arr) { yie...

OK, the code examples are good ... and I get where you are going with this.

By "practical," I am trying to find actual use-cases ... not just "Hello World" type of code.

I'm not going to bootstrap an entire project for an example, but I can give you a basic use case for them:

  1. You'd want to play and pause loops if someone hit a pause button on an animation and the loop is a tween.
  2. You'd want an infinitely looping array for things like carousels or lists of data which wrap around
  3. You'd want to iterate over objects to check if any of its properties include a specific value. You get the array and then check with .includes()
  4. You'd want a map if you have objects already and want to keep the object like structure, but also want to check if properties exist on an object that are falsy.

I hope you understand that I'm not going to code these entire use cases. It's be much too long for a comment and I'd sooner make my own post than a comment with the full code.

This is more than enough ... if you are OK with it, I'll do a follow-up post to this one with your examples included (attributed to you, of course) down the road.

Sounds good.
If you want one or two more, Dr. Axel Rauschmayer provides some use cases with async code

Awesome ... thanks for the additional references!

code of conduct - report abuse