1

I am using Datamaps, and have a JSON file of coordinates I wish to map. I am having issues making functions wait for each other. Why is it that one finishes before even calling the functions that are in its body?

Here are some code snippets trying to get a list of longitude values:

$(document).ready(function  make_bubbles() {
    list = parse_data();
})

function parse_data(){
    var longitude = new Array();
    make_array(longitude);
    return longitude;
}

function make_array(input_list){
    d3.json("data/parsed_book_data.json",function(error,data){
        if (error) return console.warn(error);
        for(var i = 0; i < data.length; i++)
        {
            if(data[i]["lng"]) {
                input_list[i] = data[i]["lng"];
            }
        }
    });
}

Update: After tweaking the above functions, I now have...

$(document).ready(function  make_bubbles() {
    var list;
    list = parse_data();
    console.log(list + "SIZE: " +list.length)
}

And it prints out the list in the console, but it says the size is 0, why is this?

  • Return the array from make_array – artm Nov 17 '14 at 00:19
  • @artm OP is passing an array and populates it, the problem is asynchronicity not the retuned value of the function. – Ram Nov 17 '14 at 00:24
  • @Vohuman If the code is changed to `var longitude = make_array();` and `make_array(){ var longitude = new Array(); ..... return longitude;`, it'll fix the problem of function not waiting the other function. – artm Nov 17 '14 at 00:28
  • @Vohuman You're right, wasn't paying attention to the `.json` call. Thanks. – artm Nov 17 '14 at 00:33
  • Just to clarify, the call `d3.json` behaves asynchronously like the `$.ajax` call in the linked duplicate question. – musically_ut Nov 17 '14 at 01:23

0 Answers0