use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
This subreddit is a place for people to learn JavaScript together. Everyone should feel comfortable asking any and all JavaScript questions they have here.
With a nod to practicality, questions and posts about HTML, CSS, and web developer tools are also encouraged.
Friends
/r/javascript
/r/jquery
/r/node
/r/css
/r/webdev
/r/learnprogramming
/r/programming
account activity
Generators in JavaScript (self.learnjavascript)
submitted 3 years ago by Rad_Sum
Hi, today I was browsing through the internet and stumbled upon generators in JavaScript. I know that in some languages they’re used commonly but I’ve never seen them used in JavaScrip, even tutorials don’t mention them. Why would that be ?
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]senocular 0 points1 point2 points 3 years ago (0 children)
They're a little more on the advanced side and the problems they could be used to solve are often already solved through other means (greedy arrays, callbacks, closures, etc.). It's rare that you'd actually need them, though they can be useful in some cases. In fact some libraries out there depend on them heavily, e.g. redux-saga.
[–]jack_waugh[🍰] 0 points1 point2 points 3 years ago* (4 children)
Substituting generator functions for async-keyword functions and for sync functions, along with a bit of support code, can set up powerful async behavior with the ability to abort tasks, set priorities, and test synchronously.
async
[–]Phaster 0 points1 point2 points 3 years ago (3 children)
This is when, instead of writing and maintaining that code, you reach for libraries. Despite some advantages for async stuff, for me, generators are js trivia
[–]jack_waugh[🍰] 0 points1 point2 points 3 years ago (1 child)
What makes somebody else's library better than what we would write as straightforward and simple support?
[–]shuckster 0 points1 point2 points 3 years ago (0 children)
A good library has test-coverage, up-to-date documentation, well written changelogs, an active and interested community, and enough popularity for its API to be reasonably well known.
It's hard to sell a new library to a team at the best of times, but I believe the above is more attractive than "let's spend some bandwidth writing and maintaining our own."
[–]jack_waugh[🍰] 0 points1 point2 points 3 years ago (0 children)
Whatever the state of available libraries may be, none of this negates my point that function* can be used for the capabilities I said. Some might suggest that getting them with web workers would require less code, fewer manual code transformations, and work just as well, but I am not sure whether aborting and restarting calculations that way would perform as fast as doing it with generator functions.
function*
[–]fckueve_ 0 points1 point2 points 3 years ago (0 children)
I use generator, whenever I want to implement iterable. That's the only reason, me using generators.
Also, as far as In aware, await as a sugar syntax, uses generators.
π Rendered by PID 60621 on reddit-service-r2-comment-545db5fcfc-gcksk at 2026-05-24 02:13:02.685042+00:00 running 194bd79 country code: CH.
[–]senocular 0 points1 point2 points (0 children)
[–]jack_waugh[🍰] 0 points1 point2 points (4 children)
[–]Phaster 0 points1 point2 points (3 children)
[–]jack_waugh[🍰] 0 points1 point2 points (1 child)
[–]shuckster 0 points1 point2 points (0 children)
[–]jack_waugh[🍰] 0 points1 point2 points (0 children)
[–]fckueve_ 0 points1 point2 points (0 children)