Skip to content Skip to sidebar Skip to footer

How To Reject Promise On D3 Xhr Request Timeout?

I want to set a timeout value so that if the server does not respond in that particular time frame then UI should move ahead and not wait for response. I have used the below syntax

Solution 1:

Since d3 v3 does not support time out, you can timeout like this:

var url = "https://mysafeinfo.com/api/data?list=englishmonarchs&format=xml";
let pr = new Promise(function(resolve, reject) {
  var xhr = new XMLHttpRequest();
  xhr.responseType = 'document';
  xhr.overrideMimeType('text/xml');
  xhr.open('GET', url, true);
  xhr.timeout = 1; // time in milliseconds
  xhr.onload = function(d) {
    console.log("load", xhr.responseXML)
    resolve(xhr.responseXML)
  };

  xhr.ontimeout = function(e) {
    console.log("timeout")
    reject({message: "I got timed out"});    
  };

  xhr.send(null);
});
pr.then(function(data) {
  console.log(data)
}, function(err) {
  console.log(err)
})

Giving a higher timeout working code here

If you use d3.v4 you can use timeout working code here


Post a Comment for "How To Reject Promise On D3 Xhr Request Timeout?"