3 ms·
Generators (yield) look very nice, especially when combined with deferred functions. The nicer scoping of let/const and yield are the two things I prefer of gj
by joeshaw 15y ago
Generators (yield) look very nice, especially when combined with deferred functions. The nicer scoping of let/const and yield are the two things I prefer of gjs (which is built on top of Spidermonkey, which supports those features) over Node.
- Sephr 15y agoYou can actually combine generators with pseudo-deferred functions today in normal JavaScript 1.7+. For example, see async.js (https://github.com/eligrey/async.js https://github.com/eligrey/async.js). Disclaimer: I wrote the library.
- joeshaw 15y agogjs has something similar: http://git.gnome.org/browse/gjs/tree/modules/promise.js http://git.gnome.org/browse/gjs/tree/modules/promise.js You end up writing code like: function doAsyncStuff() { let result = yield someAsyncCall(); result = yield someOtherAsyncCall(result); if (result) { Promise.putReturnFromGenerator("foo"); } else { throw new Error("No result!"); } } let promise = Promise.fromGenerator(doAsyncStuff); promise.get(function(result) { print(result); // -> "foo" }, function(error) { print(error); // -> exception toString() -> "No result!" }); The nice thing is the synchronous feel of doAsyncStuff() although it's actually pausing at each yield, waiting for a callback, and continuing execution from there. It would seem as though Traceur's deferred functions replace the need for the generators by doing the continuation through the "await" keyword.