JavaScript Generators




Learn about JavaScript Generators and how to use them to write asynchronous code in a synchronous fashion.

Code examples from this video: https://github.com/shama/letswritecode/tree/master/javascript-generators

Original source


25 responses to “JavaScript Generators”

  1. For those who don't know what the done is in ( {value: somevalue, done:false} ) generator returned object:

    It is just a property to let you know if there are any more elements to iterate over or not. It will return true if iterator is at the end of elements are no more elements are there to return and false if there are still elements left to be iterated.

  2. @5:55, how does `kind` become bound to 'ferocious'…? So after the first bear.next() call, `var kind = ` is left hanging undefined until the next call…? If you pass in a list of arguments, would `kind` become an array or something?

  3. To be honest I really don't like this. Not the video, but generators. They are hiding something from us, and replace details with some sort of a black magic thingy. Do people really suffer that much with async processing of data?

    I have a question as well: When you have a function like your run() function. If I want to use it, what should I expect? procedural processing of it, or async? I somewhat understand most of these basic tutorials, but I absolutely can not figure out how these fit in with any existing code. :S

  4. Just realized how works your 'run' function. I think, you missed to explain very important thing that 'yield' always immediately returns object(let's call it 'singleObject'), even if async operation hasn't completed yet. but when async operation completed then generator append some data to value property of 'singleObject'. I doesn't check it, it's only my own speculation, but I think it is actualy.

  5. Hey Kyle, just wanted to say thanks for the awesome vids. I've watched a lot of them. I shared this one on Reddit to hopefully get you some more love. Keep it up. They are awesome. Love the simple format.

  6. one thing I was expecting with

    var kind = yield 'grizzly'
    yield kind + ' polar'

    was it will print 'grizzly polar ' . I was also not sure how the generators are accepting the values .
    Probably due to new to node 🙂 .

    Thank you for the videos.

Leave a Reply