# Help with XMLHttpRequest

**URL:** <https://forum.freecodecamp.org/t/help-with-xmlhttprequest/419697>\
**Category:** General\
**Created:** [September 8, 2020, 9:57pm UTC](https://forum.freecodecamp.org/t/help-with-xmlhttprequest/419697 "2020-09-08T21:57:32Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ganeshh123](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ganeshh123/32/181377_2.png) [@ganeshh123](https://forum.freecodecamp.org/u/ganeshh123)\
**Post date:** [September 8, 2020, 9:57pm UTC](https://forum.freecodecamp.org/t/help-with-xmlhttprequest/419697/1 "2020-09-08T21:57:32Z")

</div>

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:

```auto
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')

```

 ![image](https://global.discourse-cdn.com/freecodecamp/original/3X/c/0/c087bffa50f88651bae51105ef6d39a34842621a.png)  
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.

```auto
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')

```

 ![image](https://global.discourse-cdn.com/freecodecamp/original/3X/3/8/381e9eaea81d35e80dc585ea446af97e5bdd1bbe.png)

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?

---

<div class="post-metadata">

**Author:** ![samolex](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/samolex/32/166737_2.png) [@samolex](https://forum.freecodecamp.org/u/samolex)\
**Post date:** [September 8, 2020, 10:57pm UTC](https://forum.freecodecamp.org/t/help-with-xmlhttprequest/419697/2 "2020-09-08T22:57:15Z")

</div>

> [@ganeshh123](#):
>
> 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.

---

<div class="post-metadata">

**Author:** ![camperextraordinaire](https://avatars.discourse-cdn.com/v4/letter/c/5f9b8f/32.png) [@camperextraordinaire](https://forum.freecodecamp.org/u/camperextraordinaire)\
**Post date:** [September 8, 2020, 11:54pm UTC](https://forum.freecodecamp.org/t/help-with-xmlhttprequest/419697/3 "2020-09-08T23:54:02Z")

</div>

> [@ganeshh123](#):
>
> 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.

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.

```auto
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`.

---

<div class="post-metadata">

**Author:** ![ganeshh123](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ganeshh123/32/181377_2.png) [@ganeshh123](https://forum.freecodecamp.org/u/ganeshh123)\
**Post date:** [September 9, 2020, 8:58am UTC](https://forum.freecodecamp.org/t/help-with-xmlhttprequest/419697/4 "2020-09-09T08:58:01Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![ganeshh123](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ganeshh123/32/181377_2.png) [@ganeshh123](https://forum.freecodecamp.org/u/ganeshh123)\
**Post date:** [September 9, 2020, 8:59am UTC](https://forum.freecodecamp.org/t/help-with-xmlhttprequest/419697/5 "2020-09-09T08:59:36Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![ganeshh123](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ganeshh123/32/181377_2.png) [@ganeshh123](https://forum.freecodecamp.org/u/ganeshh123)\
**Post date:** [September 9, 2020, 12:47pm UTC](https://forum.freecodecamp.org/t/help-with-xmlhttprequest/419697/7 "2020-09-09T12:47:42Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [June 1, 2021, 6:41pm UTC](https://forum.freecodecamp.org/t/help-with-xmlhttprequest/419697/8 "2021-06-01T18:41:07Z")

</div>

Closed on June 1, 2021, 6:41pm UTC


