Your
Sti
function returns before the inner
async
function has completed.
You can't write a sync-over-async wrapper in Javascript. You need to make the
Sti
function
async
, and change the calling code to
await
the result.
(async function(){
async function Sti(addr, tag, id, tt) {
let response = await fetch(...);
let text = await response.text();
return text;
}
let then = await Sti("https://stackoverflow.com", "div", "left-sidebar", "html");
document.getElementById('h3').innerHTML = then;
})();
You'll have two other issues running that code on TryItEditor:
- You're trying to load an
http:
URL from an https:
site. Your browser will probably block that request. - The URL you're trying to load doesn't allow cross-origin requests.