I want to have a loading screen where the current state is displayed while resources are loading in the background. Therefore I use an async function. I don't know much about async in JavaScript but in C# and I want to use it the same way so I just call the function.
Here is the relevant code:
// main.js
async function loadAssets() {
var loadingInfo = document.getElementById("loading-info");
loadingInfo.innerHTML = "Loading entity data..."
entityDB = JSON.parse(FileReader.read("https://my-domain.com/data/entity-db.json"));
loadingInfo.innerHTML = "Done...";
}
// file_reader.js
class FileReader {
static read(fileName) {
try {
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET", fileName, false);
xmlhttp.send();
if (xmlhttp.status == 200)
return xmlhttp.responseText;
return null;
} catch (error) {
return null;
}
}
}
The problem is that I never see the loading text. The UI freezes for 4 seconds and then the text Done is displayed. Am I misunderstanding async in JavaScript?