I have a function that prints out text from an element, followed by text from an element that is requested from a different webpage.
function together(){
var finalString ="";
var x = document.getElementsByClassName("h-userinput");
for (var i = 0; i < x.length; i++) {
var text = x[i].innerText || x[i].textContent;
console.log(text+"\n");
var query = "#ctl00_ContentPlaceHolder_ResultsGrid_TB > tr:nth-child(bbcc) > td.total-score-column";
query = query.replace("bbcc",i+1);
var poop = ((document.querySelector(query)).onclick.toString());
text = poop.replace("javascript:location.href=\'","");
text = text.replace("function onclick(event) {","");
text=text.replace("}","");
text = text.replace("\';","");
text = "https://bartholomew.itslearning.com"+text;
var url = text;
fetch(url)
.then(res => res.text())
.then((responseText) => {
const doc = new DOMParser().parseFromString(responseText, 'text/html');
console.log(doc.querySelector("#qti-choiceinteraction-container > tbody > tr.checkedrow > td.answer > div > label > span > p")+"\n");
})
.catch((err) => {
// There was an error, handle it here
});
}
//console.log(finalString);
}
The problem is that when I run this, it takes a moment to get a reply from the other webpage, but the loop continues to execute without getting the reply and printing out the result. I would like to wait until the element is printed out before moving on to the next iteration of the loop.