I'm struggling with creating a method that should return an array, but for some reason, the returned value is empty. I suspect it's because the return statement is executed before the local variable is properly set. Perhaps I am approaching this from the wrong angle. Does anyone have any suggestions on the best practice or pattern to achieve this? See code snippet below.
// THE JAVASCRIPT METHOD IN QUESTION
this.getAllMediaTypes = function() {
// DECLARATION OF LOCAL VARIABLE
var allMediaArr = [];
// API CALL TO FETCH DATA
_thisObj.apiCall(_dataUrl + '/media.json?max=' + _thisObj.maxRecords +'&callback=?', function(){
allMediaArr = _apiData.results;
console.log(allMediaArr); //THE CONSOLE LOG SHOWS THE CORRECT DATA
});
// DESIRED RETURN VALUE
return allMediaArr; // HOWEVER, THIS IS RETURNING EMPTY
}