Bruno, wouldn't yield* work here to delegate the inner yields?
Sent from my Windows Phone
________________________________
From: Bruno Jouhier<mailto:bjouhier at gmail.com>
Sent: 7/15/2013 4:12 PM
To: es-discuss<mailto:es-discuss at mozilla.org>
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.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20130716/442255da/attachment.html>
domenic at domenicdenicola.com (2013-07-19T15:35:39.678Z)
Bruno, wouldn't `yield*` work here to delegate the inner yields?