1

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.

8
  • Your code will not be successfully parsed as it is. Commented Apr 18, 2015 at 11:49
  • @thefourtheye I missed some brackets when copying and pasting, updated now. Commented Apr 18, 2015 at 11:52
  • You don't need to use async.each to synchronously loop over the data? In fact it doesn't even work as you never call the "next" callback. Commented Apr 18, 2015 at 13:01
  • What is looplength? Commented Apr 18, 2015 at 13:06
  • @Bergi Just a variable set for the length of the loop Commented Apr 18, 2015 at 13:07

1 Answer 1

1

The problem is that you have two asynchronous things going on (the lupus "loop" and the T.get calls) and essentially no coordination between them.

Because the T.get will be asynchronous, I wouldn't use lupus (ugh!) here at all:

var index = 0;
var maxId;
next();
function next() {

  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);
    });

    if (++index < loopLength) {
      next();
    } else {
      console.log('All done!');
    }
  });
}

There are a couple of unrelated things in the code that don't look right:

  1. You're using maxId when you've never assigned a value to it, in the first call to T.get. Seems like you want some kind of initial value there.

  2. You're throwing an error out of the T.get callback. Does the documentation for T.get tell you it will do something useful with that error? If not, you probably want to do something else. Throwing there won't, for instance, stop the loop in your original code (it will with the code above).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.