In my javascript code, I use the fetch() command to receive some data from my php file:
fetch('php/get_comm.php').then(response => response.text()).then(data => {
communities = JSON.parse(data);
});
Now there is some more code to be executed that uses the data acquired using fetch.
Since fetch is an asynchronous function, the rest of the script starts executing before the fetch command finishes, hence they cannot use the variable "communities".
Currently I'm using this trick as a work around:
fetch('php/get_comm.php').then(response => response.text()).then(data => {
console.log(JSON.parse(data));
communities = JSON.parse(data);
continueScript();
});
function continueScript() {
// Rest of the script.
}
This seems like a very messy way of doing this. Is there any other more clean way to accomplish what I want?