Ron Buckton (2013-07-16T00:06:13.000Z)
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<mailto:rbuckton at chronicles.org>
Sent: ‎7/‎15/‎2013 5:02 PM
To: Bruno Jouhier<mailto:bjouhier at gmail.com>; es-discuss<mailto:es-discuss at mozilla.org>
Subject: RE: generators vs forEach

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/96124714/attachment-0001.html>
domenic at domenicdenicola.com (2013-07-19T15:35:50.868Z)
I assume you are referring to something like Q.async/Q.spawn to turn the generator into an async function using promises?