I'm trying to pull the links I need from another site and shove them into an array. It seems to be doing everything right, but the array turns out to be strange. When debugging, this is what happens:

enter image description here

Why is it throwing me into a new file with a single variable? I encounter this problem already in the second project, but for the first time I was sure that this was due to bubbling. This time there are clearly no mouse events and the like. Why is it throwing me into another file and debugging is interrupted? (I can’t walk the code further)

Code:

function getPostsLinksArray(homeUrl) { var url = "https://www.thesimsresource.com/downloads/browse/category/sims4/order/downloads/"; var itemLink = "", data = "", postLink = "", postLinks = []; var xhr = new XMLHttpRequest(); xhr.open('GET', url, true); xhr.responseType = 'document'; xhr.onload = function () { if (xhr.readyState === 4) { if (xhr.status === 200) { itemLink = xhr.response.getElementsByClassName("item-link"); for (var i = 0; itemLink.hasOwnProperty(i); i++) { postLink = homeUrl + itemLink[i].getAttribute("data-href"); postLinks.push(postLink); } } } }; xhr.send(); console.log(postLinks[0]); 
  • Because it is an asynchronous function, with the heat of the heat, with an event loop. Where do you think the trace should go next? At the next random event? - vp_arth
  • one
    I forgot that the request is asynchronous ... Thank you - Jenifer Alderson
  • @vp_arth then this is why, at the beginning of the cycle, my array is already full, when in theory should it be empty? because it was filled with a request that is in the code below? - Jenifer Alderson
  • It seems that in vain you make several requests in one function, and even push the result into a common variable) - vp_arth
  • @vp_arth If you say so, then I will be corrected now. And what would I control the asynchronous request, is it better to use promises or is there some other way? - Jenifer Alderson

0