4 ms·
The `yield` operator is entirely about control flow. I'm not sure how you can see generators as being "like arrays" and not about control flow. Generators are r
by tree_of_item 12y ago
The `yield` operator is entirely about control flow. I'm not sure how you can see generators as being "like arrays" and not about control flow. Generators are related to continuations, not arrays.
It's this extra flexibility in control flow that lets you write nicer asynchronous code.
- jkrems 12y agoGenerators can definitely be used as a tool for control flow - just like arrays. But the thing that actually provides the control flow part is not the generator - just like `async.series` is providing control flow and not the array you pass in. I certainly am not objecting to using generators as one way of modeling flow. The only thing I said was "don't pretend that they somehow replace Promises". Because they don't. The solution I favor is ES7 async functions, which actually is defined in terms of Promises (async abstraction) + spawn (control flow) + generators (model/"data structure"). There's no way to use just generators and nothing else and get async control flow going. That's not how generators work. And `yield` is not "about control flow". It's about lazy sequences. And one of the many possible ways to use lazy sequences is to model control flow, when combined with a control flow library.
- ubernostrum 12y agoI think a lot of your comments are directed at JavaScript specifically and not the concept of generators in general. Especially if you were to look around at other languages' implementations of generators :)
- pdubroy 12y agoI believe it's pretty common in the programming language literature to refer to `yield` a delimited control operator. E.g., see http://parametricity.net/dropbox/yield.subc.pdf http://parametricity.net/dropbox/yield.subc.pdf, which says "yield...turns out to be a delimited control operator of comparable expressive power to shift/reset". A good hint that you're dealing with a control statement/operator is if it's responsible for transferring execution to another place in the code: e.g., `if`, `for`, `while`, `throw`, etc. Saying that "yield is not about control flow, it's about lazy sequences" seems like saying "functions are not about control flow, they are about producing values".