0
request.onload = function () {
   let avg = JSON.parse(this.responseText);
};

This is my side. I have declared a global variable elsewhere. This function is supposed to update the variable so I can use the "updated" 'avg' elsewhere. Can anyone help me to return the variable outside the function?

Zia Yamin
  • 942
  • 2
  • 10
  • 34
D.Martin
  • 3
  • 3
  • You need to return that avg variable? – Aggestor Jan 24 '21 at 08:32
  • Put the code that needs that value inside your `onload` function and/or call other functions from there and pass the value. The value will become available *on load*, **sometime later**. You cannot return it *right now*. Other code that needs the value needs to await the `load` event to happen first, which is exactly why you attach a *callback function* to it. – deceze Jan 24 '21 at 08:39

0 Answers0