This code (modified from original to remove proprietary code) is used to initialize an analytics tracker. It is suggested to be initialized as shown below:
const app = window[NAMESPACE];
if (!app.initialized) {
const script = document.createElement('script');
script.async = true;
script.src = `${script_url}?namespace=${NAMESPACE}`;
document.getElementsByTagName('script')[0].appendChild(script);
app.initialized = true;
}
Assume that this simply retrieves a bunch of functions into that namespace, including the function foo()
.
We later call foo()
...
if (app.initialized) {
window[NAMESPACE].foo(....)
}
It seems to work, but I'm skeptical -- can foo()
end up being called before the script is finished being retrieved? Before foo()
even exists? And therefore app.initialized
is actually somewhat meaningless?
edit: talked to the team suggesting it. Yes, it would possibly be a race problem. But the code I omitted before this section (because I thought it was irrelevant) sets up function stubs in the namespace that queue any function calls (i.e., adds them to an array) that are later executed after the script is loaded.