I'm a relative js newbie. As such, I'm not well versed in the more advanced concepts like asynchronous javascript etc. But I'm trying to learn. Anyway, as part of my learning, I'm experimenting if it's possible to wait for a function inside another file to become available with a simple while loop?
If not, why not?
parseQuery() function inside <script> tags inside the body of an .htm
file.I'm keying off of one of the suggestions in this post to check if the function is undefined: Test for undefined function in Javascript
Here's my basic while loop approach:
// ... earlier code gets a handle on the iframe element
// ... and I pass the query params to it with setAttribute
// we now need to wait for the parseQuery() function to become available
let i = 0;
while (typeof parseQuery === undefined){
console.log('It is still not defined. Waiting ' + i);
i++;
}
console.log('After while loop, typeof ParseQuery is: ' + typeof parseQuery);
My console log after the loop always shows: "After while loop, typeof parseQuery is undefined" There's no indication it ever gets into the while loop at all. I find this strange considering it's undefined. How is that possible? I thought it go into the loop and then essentially wait until the parseQuery function became available and then it'd exit the loop once it did.
Is my condition incorrect, or is something else I'm not understanding going on?