samedi 18 avril 2015

Node.js async looping function

I am new to Node.js and am struggling to know if this is the correct way to do the following:


I am using lupus to handle the for loop, and I am querying the twitter API, I am then trying to get the largest id in the returned json, for this I am using lodash. Once I have this value I want to then run the loop again but this time with the value passed into the function. I loop through returned JSON with async.js



lupus(0, loopLength, function(n) {

var maxId;

T.get('favorites/list', {count: 200, max_id: maxId}, function(err, data, response) {
if (err) {
throw (err);
}

maxId = _.max(_.pluck(data, "id"));

async.each(data, function(file, callback) {
console.log(file)
}, function(err){
if( err ) {
console.log('A file failed to process: '+ err);
});
})


}, function() {
console.log('All done!');
});


It seems maxId never gets set so the .each loop never gets the next set of JSON. My question is am I doing this correctly, and how do I get the value of maxId from the .each function.


Aucun commentaire:

Enregistrer un commentaire