How to prohibit code execution before downloading?

Is creating your own custom events an option?

const event = new Event('myevent');

//Wait till a function finishes then... 

window.dispatchEvent(event)

//detect the event... 

window.addEventListener('myevent',function(){
//do stuff
}

Or maybe loading manager can be of use to you?