Roland
Roland

Reputation: 9711

returning a value after for loops

So, I have been trying for the past few hours to get an result out of a function after performing some for loops :

Cluster.prototype.initiate_api_data_fetching = function(username) {
var self = this,
    object = [];

return self.initiate_available_market_search(username, function(data_object){
    var json_obj = JSON.parse(data_object);
    for(var obj_key in json_obj) {
        for (var i = json_obj[obj_key].length - 1; i >= 0; i--) {
            self.initiate_market_items_data_fetching(username, json_obj[obj_key][i].site, function(data_obj){
                var json_object = JSON.parse(data_obj);
                for(var data_key in json_object) {
                    for (var j = json_object[data_key].length - 1; j >= 0; j--) {
                        object.push(json_object[data_key][j]);
                        /*log(object);*/
                    };
                };
                log(object);
            });
        };
    };
});
};

Making abstraction of all the variables and other things that make no sense to you readers, I would just like to know how can I return the object array with the data that I\m pushing in it. Everything is fine if I\m logging where the /*log(object);*/ is, but if I want to see what the object contains at the end of the function, I get an empty array.

Upvotes: 1

Views: 83

Answers (1)

Split Your Infinity
Split Your Infinity

Reputation: 4229

I suggest you add a callback to your main function and call it when done..

Cluster.prototype.initiate_api_data_fetching = function (username, callback) {
    var self = this,
        object = [];

    return self.initiate_available_market_search(username, function (data_object) {
        var json_obj = JSON.parse(data_object)
            , counter = 0;

        function done() {
            counter -= 1;
            if (counter === 0) {
                callback(object);
            }
        }

        for (var obj_key in json_obj) {
            if (!json_obj.hasOwnProperty(obj_key)) { continue; }

            for (var i = json_obj[obj_key].length - 1; i >= 0; i--) {
                counter += 1;
                self.initiate_market_items_data_fetching(username, json_obj[obj_key][i].site, function (data_obj) {
                    var json_object = JSON.parse(data_obj);
                    for (var data_key in json_object) {
                        if (!json_object.hasOwnProperty(data_key)) { continue; }

                        for (var j = json_object[data_key].length - 1; j >= 0; j--) {
                            object.push(json_object[data_key][j]);
                            /*log(object);*/
                        }
                    }
                    done();
                });
            }
        }
    });
};

PS. 1 assumption is that initiate_api_data_fetching is async.
PS. 2 Follow the advice from the commenters above to improve your code. I answered your immediate question by showing you how to synchronise async calls, but don't stop there.

Upvotes: 2

Related Questions