When is the code in the dynamically created script crammed into the main thread to execute

I want to know that when spa has been loaded, I dynamically create a script, that contains a piece of code. Since I want to use the results executed in script in the code, I need to find out at which stage the code in the dynamically created script is crammed into the main thread for execution.

Dec.23,2021

Synchronize

var script = document.createElement("script");
script.innerHTML = 'console.log(1);'
document.body.appendChild(script);
console.log(2);
// 1 2

Asynchronous

var script = document.createElement("script");
script.src = 'xxx.js';//
document.body.appendChild(script);
script.onload = function() {//script 
    console.log('script load');
}

you can listen to the onload event and call it after loading and execution is complete.

var newScript = document.createElement('script')
newScript.src = 'xxx.js'
document.body.appendChild(newScript)
newScript.onload = function() {
   // 
}
Menu