0

Below are is a snippet of code containing object constructor and a function to populate an array of said objects. The populate function uses AJAX get() to consume data from a text file into an array. The array can be accessed and used from within the scope of the function; but my question is how can I use this array outside of the scope of the function? I have tried "initializing" the array globally and return the array from the function, which should be filled with objects. However, the array is empty when I try using it outside of the function's scope. This is odd to me because I am used to programming in C++. Any help would be greatly appreciated.

<script>
var file = "names.txt";

// object constructor
function person(first, middle, last) {
    this.f_name = first;
    this.m_name = middle;
    this.l_name = last;
}

// populate object array
function populate() {
    var people = [{}];
    $.get(file, function(txt){
        var records = txt.split("\n");
        for(var i = 0; i < records.length; i++) {
            var row = records[i].split("\t");
            people[i] = new person(
                row[0],
                row[1],
                row[2]
            );
        }
    });
    return people;
}

var arr = [{}];
arr = populate();
console.log(arr);              //LOG: [object Object]
console.log(arr[666]);         //LOG: undefined
console.log(arr[666].f_name);  //SCRIPT5007: Unable to get
                               //  value of the property
                               //  'f_name': object is null
                               //  or undefined 



</script>
Jim22150
  • 511
  • 2
  • 8
  • 22
  • possible duplicate of [How to return the response from an asynchronous call?](http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-asynchronous-call) – Andreas Jun 15 '15 at 15:03

1 Answers1

0

arr = populate(); //this is called and $.get would have not finished the execution. $.get is as asynchronous call.

but the console.log statements will be executed before the arr is filled.

Use success call function to do post implementation of success .

function populate() {
    var people = [{}];
    $.get(file, function(txt){
        var records = txt.split("\n");
        for(var i = 0; i < records.length; i++) {
            var row = records[i].split("\t");
            people[i] = new person(
                row[0],
                row[1],
                row[2]
            );
        }

    });
    console.log(people);              //LOG: [object Object]
    console.log(people[666]);         //LOG: undefined
    console.log(people[666].f_name);
}
Sunil B N
  • 4,159
  • 1
  • 31
  • 52
  • Yes, moving the call inside of the function scope is what I originally had as a working prototype. But my question was how to do it out of scope. Is this not possible? – Jim22150 Jun 15 '15 at 15:46
  • You can do. Use jQuery deferred object – Sunil B N Jun 15 '15 at 16:47
  • @Sunil_B_N I have never used deferred objects in jQ library. It looks a great solution.http://api.jquery.com/category/deferred-object/ However, I found another solution here: http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-asynchronous-call using promises. I may close my question because it is too similar to other questions unless you would -- and could -- provide an example to using deferred objects as they would pertain to my code above?? Thanks in advance. – Jim22150 Jun 15 '15 at 17:39
  • you can close :) no issues.. The important thing is you got the solution.. just for your info, jQuery deferred objects also use promises :) – Sunil B N Jun 16 '15 at 05:04