If you want to use `yield` in nested functions you will have to use yield*
to and your nested functions need to be generators

```js
suspend(function* (resume) {
    var contents = yield fs.readFile('idList.json', 'utf8', resume);
    yield* each(contents.split('\n'), function* (id) {
        var info = yield
request.get('http://www.example.com?id='+id<http://www.example.com/?id='+id>,
resume);
    });
})();

// Generator friendly forEach for arrays
function* each(array, callback) {
  for (var i = 0, l = array.length; i < l; i++) {
    yield* callback(array[i]);
  }
}
```


On Fri, Jul 12, 2013 at 2:56 PM, Rick Waldron <waldron.r...@gmail.com>wrote:

>
>
>
> On Fri, Jul 12, 2013 at 5:47 PM, cpprototypes <cpprototy...@gmail.com>wrote:
>
>> I'm using node 0.11.3 with --harmony-generators to try the new feature.
>>  I found two libraries that should help use existing node callback-based
>> code with generators (suspend and galaxy).  The code I'm trying to run was
>> similar to the following:
>>
>> (using suspend)
>>
>> var fs = require('fs');
>> var request = require('request');
>>
>> suspend(function* (resume) {
>>     var contents = yield fs.readFile('idList.json', 'utf8', resume);
>>     contents.split('\n').forEach(function(id) {
>>         var info = yield request.get('http://www.example.com?id='+id,
>> resume);
>>     });
>> })();
>>
>> (using galaxy)
>>
>> var galaxy = require('galaxy');
>> var fs = galaxy.star(require('fs'));
>> var request = galaxy.star(require('request'));
>>
>> function* main() {
>>     var contents = yield fs.readFile('idList.json', 'utf8');
>>     contents.split('\n').forEach(function(id) {
>>         var info = yield request.get('http://www.example.com?id='+id);
>>     });
>> };
>>
>> galaxy.unstar(main)(function(err, result) {
>>     console.log('done');
>> });
>>
>> Using either library, when node tries to execute the get request ("var
>> info = yield request.get...") it exits with the following error:
>>
>> SyntaxError: Unexpected identifier
>>
>> And the error highlights the "request" part in "var info = yield
>> request.get..."  I'm guessing that the creation of the new function scope
>> in the forEach is somehow causing an issue.  But I'm not sure why it's not
>> working.
>>
>
>
> `yield` may not be used in the body of a regular function.
>
> Rick
>
>  --
> --
> Job Board: http://jobs.nodejs.org/
> Posting guidelines:
> https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
> You received this message because you are subscribed to the Google
> Groups "nodejs" group.
> To post to this group, send email to nodejs@googlegroups.com
> To unsubscribe from this group, send email to
> nodejs+unsubscr...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/nodejs?hl=en?hl=en
>
> ---
> You received this message because you are subscribed to the Google Groups
> "nodejs" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to nodejs+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>
>

-- 
-- 
Job Board: http://jobs.nodejs.org/
Posting guidelines: 
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to nodejs@googlegroups.com
To unsubscribe from this group, send email to
nodejs+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

--- 
You received this message because you are subscribed to the Google Groups 
"nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to nodejs+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to