I thought about yield* but it was not available at the time I wrote this. But it does not fit the bill because it deals with the synchonous part of the generator dance, not the async part. My run loop is if a bit more than the yield* loop (but not much more). The differences are the tests that deal with the PENDING case ( https://github.com/bjouhier/galaxy/blob/master/lib/galaxy.js lines 83 and 88).
I'm not using Q. I wanted to have minimal runtime overhead so I designed it so that it does not allocate any extra objects. It's all done with a simple loop. 2013/7/16 Ron Buckton <[email protected]> > I assume you are referring to something like Q.async/Q.spawn to turn the > generator into an async function using promises? > > > Sent from my Windows Phone > ------------------------------ > From: Ron Buckton <[email protected]> > Sent: 7/15/2013 5:02 PM > To: Bruno Jouhier <[email protected]>; es-discuss<[email protected]> > Subject: RE: generators vs forEach > > Bruno, wouldn't yield* work here to delegate the inner yields? > > Sent from my Windows Phone > ------------------------------ > From: Bruno Jouhier <[email protected]> > Sent: 7/15/2013 4:12 PM > To: es-discuss <[email protected]> > Subject: Re: generators vs forEach > > There is no need to CPS transform functions and there is no need for > deferred functions either. It can all be done with today's generators and a > little helper library. > > With the C# async/await notation: > > - The yield keyword is your "await" keyword. > - The little * in function* is your "async" keyword. > > With this you can write: > > function* asyncEach(array, fn) { > for (var i = 0; i < array.length; i++) yield fn(array[i], i); > } > > and you can call it as: > > function* myFunc(array) { > yield asyncEach(array, function*(elt, i) { > var foo = yield asyncBar(elt); > // more ... > }) > } > > Note that there is *no* helper API in all these async functions that > call other async functions; The helper API is only needed to interface this > with the classical callback world: at the very bottom of the stack when you > call low level callbacks-based I/O functions, and at the top of the stack > when the event loop runs one of your generator functions. > > The trick is that you need a clever run function to do the little > yield/next dance with generator functions that call other generator > functions. > > I've implemented this in > https://github.com/bjouhier/galaxy/blob/master/lib/galaxy.js (the run and > invoke functions) > > The only thing I don't like about it is the awkward syntax: > > - yield precedence does not work well > - yield is prefix, which does not chain well > - and yield is heavy anyway > > In short, this is a hack to get going but I'm still waiting for the full > concurrency proposal and its awesome ! syntax. > > Bruno > > > Consider the following: > > > > function* yieldEach(array){ > > array.forEach(n => { > > yield n; > > }); > > } > > > > In order for this to work, not only does `yieldEach` have to be > suspended for the inner yield, but forEach does as well. That means CPS > transforming functions based on whether they call a yielding function. > >
_______________________________________________ es-discuss mailing list [email protected] https://mail.mozilla.org/listinfo/es-discuss

