0

I have an onload function from an XMLHTTPRequest function which is configured like so.

CreateExcel()
  {
    let url = this.GetDownloadURL();
    var req = new XMLHttpRequest();
    req.open("GET", url, true);
    req.responseType = "arraybuffer";

    req.onload = function(e) 
    {
      var data = new Uint8Array(req.response);
      var workbook = XLSX.read(data, {type:"array"});
      var worksheetName = workbook.SheetNames[0];
      var workSheet: XLSX.WorkSheet = workbook.Sheets[worksheetName];
      let s3Sheet = (XLSX.utils.sheet_to_json(workSheet, {header: 1}));
      console.log(s3Sheet)
    }
    req.send();
  }

I understand that req.onload is asynchronous, but is there a way for me to take that s3Sheet variable and access it within the CreateExcel function after the req.send() is complete?

Banani720
  • 159
  • 3
  • 17
  • No. When the `req.send()` function completes, the request was *sent*, it does not wait for the response to be loaded. – Bergi Mar 14 '20 at 20:10
  • @Bergi so once req.send() is sent then it's over? could I access by resolving it through a promise maybe? – Banani720 Mar 14 '20 at 20:42
  • 1
    The promise still would be asynchronous, and you'd need to use the values from within the `then` callback – Bergi Mar 15 '20 at 22:56
  • Haha I got it makes sense now, I’d give you a green check mark but the question got closed – Banani720 Mar 15 '20 at 23:08

0 Answers0