domenic at domenicdenicola.com (2013-07-19T15:35:21.150Z)
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:
```js
function* asyncEach(array, fn) {
for (var i = 0; i < array.length; i++) yield fn(array[i], i);
}
```
and you can call it as:
```js
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.
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/51738c5f/attachment.html>