-1

I'm trying to write a method that returns an array, but the return value is empty. I think it's because it's happening before the local variable is set. I also think I looking at this totally wrong. Can anyone tell what would be the best pattern or practice to accomplish this? Code below.

// THIS IS MY JAVASCRIPT METHOD
this.getAllMediaTypes = function() {
    // MY LOCAL VARIABLE
    var allMediaArr = [];
    // METHOD I WROTE TO MAKE API CALL
    _thisObj.apiCall(_dataUrl + '/media.json?max=' + _thisObj.maxRecords +'&callback=?', function(){
        allMediaArr = _apiData.results;
    console.log(allMediaArr);//THIS RETURNS THE RIGHT DATA IN THE CONSOLE
    });
    // WHAT I WOULD LIKE RETURNED 
    return allMediaArr;// THIS IS RETURNING EMPTY       
}
ski760
  • 151
  • 2
  • 14

1 Answers1

1

I think you are making an asynchronous call to the api. Make it synchronous.

Krishna Mohan
  • 1,612
  • 3
  • 19
  • 27