1

The distinction of between Coroutines and Generators is blurry for me.

I'd appreciate it if someone could read the article Callbacks vs Coroutines - A look at callbacks vs generators vs coroutines" at https://medium.com/code-adventures/174f1fe66127

...and then explain where and how the difference and competition (the title does use the term "vs") between generators and coroutines is illustrated/presented in this article? More generally my question is what in fact is that distinction, and does that distinction apply to how one might differ the coding pattern for handling async requests when building an app in Koa (in Node.js)?

JLS
  • 691
  • 1
  • 6
  • 10

1 Answers1

1

Generators are sometimes referred to as “semicoroutines”, a more limited form of coroutine that may only yield to its caller. This makes the use of generators more explicit than coroutines, as only yielded values may suspend the “thread”.

"generators" are lower level than coroutines. They can be used as building blocks for a coroutine lib. You can construct a "coroutine" lib if you have "generators" in the language.

ES6 provides "generators", so you can use a "coroutine" lib to implement coroutine programming in node (sequential, with try-catch instead of callback)

With a coroutine lib, you can "wait" for the async call to complete.

check: https://github.com/luciotato/waitfor-ES6

Lucio M. Tato
  • 5,639
  • 2
  • 31
  • 30
  • Hi Lucio. Thanks for the response. Two things: – JLS Apr 16 '14 at 20:03
  • 1) Referring to my original question, why the use of the term "versus" in the title of the article "...generators vs coroutines..." Is it not the proper term, or I'm not understanding things so that I can appreciate what "versus" means here? Doesn't seems it's a choice between one or the other. Coroutines seem more like the control logic for running generators, and advancing them past yields. Am I on the right track here? – JLS Apr 16 '14 at 20:03
  • 2) Along that lines, wondering if you could have a look at http://www.aaron-powell.com/posts/2014-01-18-calling-up-callbacks-with-yield.html, and tell me if the presented "runner" function (fleshed out at the end of the article) fits the bill of being coroutine. (May have a followup question per your answer) – JLS Apr 16 '14 at 20:06
  • Also, have looked at both wait-ES6, and parallel-ES6, about two weeks and, and just now The "grammar" that they allow one to code in is very nice. That said, couple of reservations, which maybe you might be able to address: 1. Aside from from your github pages, documentation and the like on the web is sparse; 2) Performance characteristic as say compared to Co and Bluebird; Probably going to use Koa for my app, which is tightly bound to Co, how would your utilities work in the Koa context, and can you show examples of that? I think Co being tied to Koa, give Co a "marketing" advantage. – JLS Apr 16 '14 at 20:13
  • (Note re prior comment: The phrasing "...say compared to Co and Bluebird; Probably going to use Koa..." should instead read as "...say compared to Co and Bluebird?; 3) Probably going to use Koa..." – JLS Apr 16 '14 at 20:21
  • 1) Yes, it is not the proper term. Title should be: "A look at callbacks vs generators & coroutines" – Lucio M. Tato Apr 17 '14 at 02:06
  • 2) "runner" is a function which uses generators to "run" coroutines. The "coroutine" is what you pass to "runner". – Lucio M. Tato Apr 17 '14 at 02:20
  • 3) Yes, Koa is tightly bound to Co. I guess wait.for can be used with Koa if you want simpler syntax, but "co" provides the same functionality and more (with a more complex syntax, also you need to "thunkify" a standard node.js async fn before you can use it with 'co') – Lucio M. Tato Apr 17 '14 at 02:23
  • Lucio - Thanks for the responses. You don't know how much your clarifying the title (for question 1) relieves a lot of confusion. Regarding q&a's "2" and "3" have followup questions: – JLS Apr 17 '14 at 04:32
  • Re "2", referring to the same article, in the block between the line "And finally we can make our generator like this:" and the CONCLUSION, we have the line of code "runner(fn);", would the "fn" be the coroutine, or the thunk "get" which is called in fn, or something else. My understanding is/was that fn is called "generator function". – JLS Apr 17 '14 at 04:32
  • (correction, should have typde: ".... or is the coroutine the thunk "get" that is called in fn, or is it something else? ..." – JLS Apr 17 '14 at 04:40
  • Re "3", Maybe not just for me, but wait-ES6's market, if you're willing to, could you expound or your response, maybe w/ side-by-side comparisons with Co (or Koa's facade of it), particularly in respect to no need to thunkify . (Maybe better to present this on wait-ES6's github page than here). I think a side-by-side comparison of how the various generator (maybe better called coroutine) libs skin various scenario cats would be helpful to many. I sense Koa will gain a large following over time, so to also do this in the Koa context would be useful, if reason could be shown to bypass Co. – JLS Apr 17 '14 at 05:03