I'm wondering if there is any way to "pause" script execution until a promise is resolved and only than continue execution of that specific script.(i know i can use .then() and in es6 i can even use await , but that's not what i'm asking). i can implement sleep function like that:
function sleep(milliseconds) {
var start = new Date().getTime();
for (var i = 0; i < 1e7; i++) {
if ((new Date().getTime() - start) > milliseconds){
break;
}
}
}
can i check on each second the promise status? or because js is single threaded this will actually freeze everything? any messy way to wait for a promise synchronously ?
Edit: My goal is to handle this situation:
function loadScript(src) {
try {
var parent = document.getElementsByTagName('head')[0] || document.documentElement;
var httpRequest = new XMLHttpRequest();
httpRequest.open('GET', src, false);
httpRequest.send();
var script = document.createElement('script');
script.type = 'text/javascript';
script.text = httpRequest.responseText;
parent.appendChild(script);
} catch (e) {}
}
this function is located inside a third party js , now i can hook the XMLHttpRequest object and intercept that call and make it async but this will mess up everything , any ideas?