0

I have this JS code:

var publicationData = new Array();
var researchers = [];
var year = [];
var title = [];
var pub = [];
var dataJson = [];

callServerAsync();

function callServerAsync(){
    $.get('Year2014.html').then(function(responseData) {
        handleData(responseData);
    });
}
function handleData(responseData){
    var htmlObject = document.createElement('div');
    htmlObject.innerHTML = responseData;
    pub = htmlObject.getElementsByClassName("julkaisu");
    getPublicationData(pub);
    getResearchersYearTitle(publicationData);
    createJson(researchers,year,title);
    console.log(JSON.stringify(dataJson)); //returns dataJson object
}

console.log(JSON.stringify(dataJson)); //returns empty

function createJson(researchers,year,title){

    for(var i = 0; i < researchers.length; i++){
        dataJson.push({ "Researchers": researchers[i],
            "Year": year[i],
            "Title": title[i]
        });
    }

}

I need to access to the value returned by the function createJson (dataJson).

I have read this link (How do I return the response from an asynchronous call?) but still couldn't understand how to solve the issue.

I would appreciate any help.

Community
  • 1
  • 1
supaplex
  • 157
  • 4
  • 18
  • http://blog.slaks.net/2015-01-04/async-method-patterns/ – SLaks Apr 12 '15 at 13:04
  • Well, you've read the other post and all answers on it, but what did you try then? Please show us your attempt at using callbacks or promises (or whatever) – Bergi Apr 12 '15 at 14:03
  • I have edited the code with my attempt. How can I reach the dataJson object outside of the handleData function? With my attempt it returns empty. – supaplex Apr 12 '15 at 15:47

0 Answers0