I have a Javascript file that has to be loaded in a body of HTML documents by my main.js file of code. there is a function in this Javascript file to be executed after this JS file has fully loaded. but the problem is that async-await doesn't wait for the js file to be fully loaded and it runs my function after await sentence sooner. my sample code :
-> main.js file :
async main() {
await Promise.all([
bodyScript("assets/js/form.js"),
]);
func_form_view(); // this function has to wait and not run untill the above js file become fully loaded.
}
bodyScript(scriptUrl) {
return new Promise((resolve, reject) => {
const scriptElement = document.createElement('script')
scriptElement.src = scriptUrl
scriptElement.onload = resolve
document.body.appendChild(scriptElement)
})
}
-> form-2.js file :
function func_form_view() {
-- // code
-- // code
.
.
.
-- // code
console.log('hi')
}
and it's why I usually get this error: "func_form_view function has not defined".
but I think anything looks right!!!