xhr-promise icon indicating copy to clipboard operation
xhr-promise copied to clipboard

Issues with multiple async requests

Open amaschas opened this issue 7 years ago • 0 comments

I am using xhr-promise in a library that requests data via a two-step process that can occur multiple times asynchronously. Each data request involves a query to an API that responds with a data URL. The library then makes a second request to fetch the data from the URL in the first response.

getQueryData(queryID) {
  let xhrPromise = new XMLHttpRequestPromise();
  return xhrPromise.send({
    method: 'GET',
    url: 'http://localhost:3002/v1/query-jobs/' + queryID
  }).then(response => {
    if (response.responseText.done) {
      return this.xhrPromise.send({
        method: 'GET',
        url: response.responseText.document_url
      });
    } else {
      return Promise.resolve(false);
    }
  });
  }

This process works fine if I execute getQueryData once. However, if I execute it twice (which means it's executing asynchronously, in parallel), the second stage of the first request returns with no data. The problem appears to occur in the call to _this._getResponseUrl() in xhr-promise. The response data is present before the call to _getResponseUrl, but once inside the function code block, the referenced xhr is empty.

It's also worth noting that while responseText is empty in object that gets returned by xhr-promise, the reponseText field in the xhr object that gets returned is populated, and JSON therein is valid. I'm currently just using the values in the xhr object in my implementation.

amaschas avatar Mar 20 '17 18:03 amaschas