Generators in JavaScript – What, Why and How – FunFunFunction #34




Generators are (kind of) pausable functions in JavaScript. Another word for them is co-routines. They are used (among other things) to manage async operations, and play very well with promises.

I’m also active on:
► Twitter https://twitter.com/mpjme
► Medium https://medium.com/@mpjme
► Quora https://www.quora.com/profile/Mattias-Petter-Johansson

Resources:

► Recursion in JavaScript

► Promises in JavaScript

► ES6 JavaScript features Playlist (videos like this one)

► Joakim Karud (Background music in episode)


Original source


29 responses to “Generators in JavaScript – What, Why and How – FunFunFunction #34”

  1. hi.funfunonction i am french and i love it your Chanel…sort for m'y Bad english…à have a Dreamcast make possible m'y Idea…..à software hack fonction of language binary code!!!?
    m'y goal make plateforme inédit realy most Speedy….to many funfun fonction on perspective ? are you busy ? i chearch for make it ??

  2. Been trying to figure out what Generators are all about. This video makes me think they are overly complicated for no apparent reason. As mpj said in the end of the video, I too would like to see more practical uses of them.

  3. The definition of generators themselves as “pauseble functions” is great. That further explanation of them as the way to deal with an async stuff can be quiet confusing. Because only the wrappers like `co` or `redux-saga` can make this tool useful for that kind of operations which is somewhat hacky pre-async/await the way to write async code as synchronous. While at the low-level they are just an implementation of iteration protocols and can be fully unaware of something asynchronous.

    I know a lo-o-o-t of developers who believe that their purpose is mainly to keep async code “cleaner”.

    Correct me please if I’m not right and miss something.

  4. Love the video, but although recursion is a great feature, it should only be used when you can ensure the call stack is predictable and never going to exhaust the stack. A simple solution and always overlooked is to do a while around DONE then you will avoid a stack overflow problem, always try and implement with loops rather than recursion, can always handle many many more iterations.

  5. Hi MPJ, I think generators are useful in case processing parent/child nodes.
    let me explain if u deal with Scrapy (python) if I crawl something like youtube channels so I will iterate over list of channels then yield each channel video into video parser.

    Let me know ur opinion

  6. I get the use of generators and totally appreciate showing how it works, without the use of a library.
    But towards the end you kind of rushed through without much explanation to what you was doing with the recursive function.

  7. Generators have an obvious use case, which you touch on at about 25:40: they let you write procedural, step-wise code (as you have in your generator example that gets passed to `run`) that use and depend on asynchronous code as though it were synchronous.

    HUGE win for simplifying code and avoiding callback hell. There are no visibly-explicit traditional callbacks in the generator code itself; the code that runs the generator (`run`, `co`, or whatever) takes care of that nastiness for you.

    As someone who has been writing code for a living since '79(!) and generally learns (or relearns) at least one or two new languages a year, I LOVE your videos. Great attitude and presentation skills, and the periodic diversions into less-than-obviously-related topics are welcome. Keep up the great work!

  8. Hello!
    Thank you to make me finally understand co !! I never understand the co + generator thing… Now I understand that it's a early implementation of async function + await but without these keywords yet! 🙂
    I begin to use async + await (as it's supported by chrome 55), it's just awesome how the returned values are automagicaly transformed in Promise!

    By the way, I still don't understand well the use case of generators, I mean, the original use without co or any fake async function lib?
    I'm a bit disapointed, I thought you, funfunfunction, had given me some clues but you finally only speak of co (what is good still).
    What about generators with for .. of loops iterable interface and Symbol.iterator ?

Leave a Reply