I responded a bit too quickly. To make it work with galaxy you need to
change the following:
* forEach -> forEachStar
* function(id) -> function*(id) for the callback
* add a yield to the forEachStar call (this is a function* so you need to
yield on it)
This will get you started but you will hit a few problems with the request
module. The first one is that request is a function. So the
galaxy.star(request) call will wrap the request function instead of
wrapping request.get. You can get around it by calling
galaxy.star(request.get) but then you'll run into another problem, which is
that the get callback receives 3 arguments instead of 2. So you need a
little wrapper around it. An alternative is to use galaxy's stream module
(see the tutorial).
Here is a revised version that works:
var galaxy = require('galaxy');
var fs = galaxy.star(require('fs'));
var getWrapper = function(url, callback) {
return require('request').get(url, function(err, response, body) {
callback(err, { response: response, body: body });
})
}
var get = galaxy.star(getWrapper);
function* main() {
var contents = yield fs.readFile('idList.json', 'utf8');
yield contents.split('\n').forEachStar(function*(id) {
var result = yield get('http://www.example.com?id='+id);
console.log("body=" + result.body);
});
};
galaxy.unstar(main)(function(err, result) {
if (err) throw err;
console.log('done');
});
On Saturday, July 13, 2013 10:48:43 AM UTC+2, Bruno Jouhier wrote:
>
> yield is only valid inside a function*. This is why galaxy has a 'Star'
> variant for all the array methods that take a callback. So, it should work
> if you replace forEach by forEachStar
>
> Bruno
>
> On Friday, July 12, 2013 11:47:28 PM UTC+2, cpprototypes 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.
>>
>>
>>
--
--
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 [email protected]
To unsubscribe from this group, send email to
[email protected]
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 [email protected].
For more options, visit https://groups.google.com/groups/opt_out.