Help with XMLHttpRequest

I’m using an XMLHttpRequest to fetch and log symbols and prices of 2 stocks.

In this first example, I am running it synchronously, and it works fine, and manages to log information on both stocks:

let xhr = new XMLHttpRequest()
let requestUrl

let processStock = (name) => {
  requestUrl = 
'https://stock-price-checker-proxy--freecodecamp.repl.co/v1/stock/' + name + '/quote'
  xhr.open('GET', requestUrl, false)
  xhr.send()
  let apiResponse = JSON.parse(xhr.responseText)
  console.log(apiResponse['symbol'])
  console.log(apiResponse['latestPrice'])
}

processStock('goog')
processStock('msft')


However, I know that this is a bad approach, and would like to make it asynchronous.

So, I’ve rewrote it asynchronously like this. However, only the second stock gets logged.

let xhr = new XMLHttpRequest()
let requestUrl

let processStock = (name) => {
  requestUrl = 'https://stock-price-checker-proxy--freecodecamp.repl.co/v1/stock/' + name + '/quote'
  xhr.open('GET', requestUrl, true)
  xhr.onload = () => {
    let apiResponse = JSON.parse(xhr.responseText)
    console.log(apiResponse['symbol'])
    console.log(apiResponse['latestPrice'])
  }
  xhr.send()
}

processStock('goog')
processStock('msft')

Async functions confuse me in general. As per my understanding, the two processStock executions should run independently of each other, ensuring both stocks’ data gets logged, but this does not seem to be the case.

Can someone help me understand why only the second stock gets logged?

I am a beginner though, but the request failed - I don’t know why.

You could check the network tab in your dev tool to see it.

Two calls to the function are executed, but since you defined xhr globally, it gets overwritten by the second call before the first loads.

To fix this, move the initialization inside the processStock function.

const processStock = (name) => {
  const xhr = new XMLHttpRequest();
  const requestUrl = 'https://stock-price-checker-proxy--freecodecamp.repl.co/v1/stock/' + name + '/quote'
  xhr.open('GET', requestUrl, true)
  xhr.onload = () => {
    let apiResponse = JSON.parse(xhr.responseText)
    console.log(apiResponse['symbol'])
    console.log(apiResponse['latestPrice'])
  }
  xhr.send()
}

processStock('goog')
processStock('msft')

With it inside the function, you actually create two independent instances of XMLHttpRequest.

Had a look at the network tab, and yeah it has. I assume it’s because of what the next answer says, that it was overwritten before it could be completed. Thanks for your help!

Thank You!

This makes sense. Is it okay to use the same name xhr for both instances since they might both be running at the same time. Are the two executions done at different memory locations?

xhr is a new instance at each call to the function. Because of closure, JavaScript makes sure that what ever variables you declare inside the function, will be executed in their own scope. Even thought the variable xhr is used in each call, it has a completely different value because it is a new instance of This is what makes them independent of XMLHttpRequest.

1 Like

Thanks a lot for your help! Cleared up some concepts for me :slight_smile: