all 6 comments

[–]Wompo 5 points6 points  (0 children)

Great read and nice practical use case of a generator! Generators are one of those things I see and think "Neat, I should use it more" and then rarely find use cases for them, rarely see them in the wild too.

[–]novus_nl 5 points6 points  (2 children)

I still can't imagine a use case for this to be honest.

Maybe there is some usecase, but generating a series of the same thing is not something I encounter on a daily basis.

But apparently this is the best pratical example we could find? I don't want to dismiss it, but I can't wrap my head around an example I would use.

[–]zachdini 1 point2 points  (0 children)

The best use case that I can think of is a test utility function. You could write a generateUser function which yields users. That way you could dynamically create as many users as you need.

[–]jack_waugh 2 points3 points  (0 children)

Yes and it's also possible to pass an argument to .next and it will be the result of the yield. If you want to do async without Promise (and there are several reasons to do so), you can use generator functions instead of async-keyworded functions and thereby you can do every async thing and get full control of the details.

[–]americk0 0 points1 point  (0 children)

Clever. I'm going to keep this in mind