1

I'm using Node.JS to iterate through some data and push that data to an array. However, console.log does not seem to show any changes that I've made. I'd like to be able to process the data in twitter_ids after the function is done pushing data to it.

I'm wondering if it's due to a misunderstanding of Node.JS's asynchronous nature?

var twitter_ids = []

function sendResults (twitter_ids){
    return function(data){
        for (var num in data['results']) {
            T.get('users/lookup', { screen_name: data['results'][num]['twitter_id'] }, function(err, data, response) {
                twitter_ids.push(data[0]['id']);
            });
        }
    console.log(twitter_ids);
    }
}

sunlightResults.call(sendResults(twitter_ids));
1
  • 1
    The function is asynchronous. The array won't actually be populated until the operations complete. Commented Jun 24, 2014 at 0:21

2 Answers 2

2

Your problem is that you are printing to the console before T.get() has retrieved any data.

If you need to wait until multiple callbacks have been called (as per your example), I usually use a helper library function like async.eachSeries(). If you want to do it yourself, something like recursion can be your friend, but might be a little convoluted:

function lookup(list, index, output, finished) {
    if(index >= list.length) { return finished(output); }

    var num = list[index];
    T.get('users/lookup', { screen_name: data['results'][num]['twitter_id'] }, function(err, data, response) {
        output.push(data[0]['id']);
        lookup(list, index+1, output, callback);            
    });
}

var outputList = [];
lookup(data['results'], 0, outputList, function(output){
    console.log(output);
});

I am sure some genius here can make this better an more readable, but just a super quick example.

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

Comments

0

Here's an implementation using async

var async = require('async');
var twitter_ids = []

function sendResults (twitter_ids){
    return function(data){
        async.each(data.results, function (result, done) {
            T.get('users/lookup', { 
                screen_name: result.twitter_id 
            }, function(err, data, response) {
                if (err) {
                    return done(err);
                }
                twitter_ids.push(data[0].id);
                done();
            });
        }, function (err) {
          if (err) {
              throw err;
          }
          console.log(twitter_ids);
        });
    }
}

sunlightResults.call(sendResults(twitter_ids));

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.