I am trying to read in data from a file and save it into an array using jquery's get function. However, because the get function is asynchronous, the code after the $.get function call runs and the data is undefined. How can I run the code following the $.get function call only after the call is completed and data has been returned?
I have tried setting async to false, but the console log gives the error that it is deprecated.
class CharacterDatabase{
constructor(fName){
this.fileText = readFile(fName);
this.fileText = this.fileText.split(/[\r\n]+/);
}
}
function readFile(fName){
console.log(fName);
$.get(fName, function(data){
return data;
}, "text");
}
var cd = new CharacterDatabase("text.txt");
The error:
main.js:32 Uncaught TypeError: Cannot read property 'split' of undefined at new CharacterDatabase (main.js:32) at main.js:85
is thrown in the console.
Line 32 is:
this.fileText = this.fileText.split(/[\r\n]+/);
When setting async to false the error states that synchronous XMLHTTPRequests are deprecated.