Hacker Newsnew | past | comments | ask | show | jobs | submitlogin
A Study on Solving Callbacks with JavaScript Generators (jlongster.com)
97 points by jlongster on June 5, 2013 | hide | past | favorite | 8 comments


Node.js users interested in seeing how generators interact with their async system should probably look at the Python Twisted library, which has had years to refine that pattern.


Can also check out tulip[0] which attempts to be a standardization of sort to give a common core interface to various event loop libraries, and defines a full event loop and adds coroutines on top[1].

Since it's a tentative distillation of existing systems and has an extensive design document, it might be simpler than diving into Twisted.

[0] http://www.python.org/dev/peps/pep-3156/

[1] http://www.python.org/dev/peps/pep-3156/#coroutines-and-the-...


Are generators allowed in RPython/PyPy yet? Last I checked, they were disallowed due to the difficulties of making them performant.


PyPy absolutely supports generators. Python's stdlib uses them extensively.

If you were worrying about the JIT not supporting them, it appears to just fine:

gentest.py:

  def gen():
      for i in xrange(10**9):
          yield i

  for i in gen():
      pass

  $ time python gentest.py # python 2.7

  real	1m12.956s
  user	1m12.736s
  sys	0m0.000s

  $ time pypy gentest.py # pypy 2.0.2

  real	0m5.728s
  user	0m5.708s
  sys	0m0.000s


This is awesome, thanks for the writeup. I love that Mozilla devs and Google (among many other people), keep pushing the boundaries on JavaScript. Even in just one year, its astonishing how much the web has changed..


Great, things are moving - does anybody know how far away the specs is from being finalized and when we'll see it in the browsers and not just Node?


The specification has already reached "draft", so it's usually pretty stable at that point. Firefox has actually included generators for awhile now (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guid...), but their implementation is based on JavaScript 1.7, rather than ES6. The end implementation was pretty close, but didn't include the function* syntax (I don't believe they implemented yield* either). Regarding timeline, that's obviously hard to guess at from an external perspective, but most browser vendors seem to be comfortable enough with developing against draft-level specs, so hopefully soon.


It's hard to say, but I think a year is an optimistic guess, and closer to two years more realistic. That's really not that long in terms of browser years though, and you can start building stuff on it today if you want to build a product for the future.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: