This question is not a case when something doesn't work or working wrong - I just need to be sure the following solution would be always working.
In main.js I fetch a JSON string from a JSON file and after that I enqueue another JS file which needs to work with the fetch result:
async function read() {
const response = await fetch('http://localhost/data.json');
return response.text();
}
let jsonObj;
read().then((jsonStr) => {
jsonObj = JSON.parse(jsonStr);
enqueueJS();
});
function enqueueJS() {
let script = document.createElement('script');
script.src = '/data_process.js';
document.body.appendChild(script);
}
I just need to be sure: will the data_process.js file have always access to the jsonObj global variable or see it regardless of the duration of the fetch request in the main.js?