techalicious
techalicious

Reputation: 441

For loop iterating over array in NodeJS but console.log won't print to terminal?

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

Upvotes: 1

Views: 905

Answers (2)

generalhenry
generalhenry

Reputation: 17319

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

Upvotes: 0

Matt Way
Matt Way

Reputation: 33141

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.

Upvotes: 2

Related Questions