如何拒绝d3 xhr请求超时的承诺? [英] How to reject promise on d3 xhr request timeout?

查看:111
本文介绍了如何拒绝d3 xhr请求超时的承诺?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想设置一个超时值,以便如果服务器在该特定时间段内未响应,则UI应该前进而不等待响应.到目前为止,我已经使用了以下语法,但是UI被挂起等待响应.如何指定一个回调以重置值并通知UI不再需要等待.

q.promise(function(resolve, reject) {
  d3.xhr(my_url)
    .header('Content-Type', 'text/xml')
    .timeout(2000)
    .send(this.my_method, my_xmlData, function(error, data) {
    })
});

我在此处读到,d3 xhr现在支持超时功能.请求超时后,我该如何使用它并执行回调?谁能告诉我如何正确使用它?我尝试使用

.ontimeout(function(){console.log('request timedout!!'}) 

但是没有用.

我使用的是Promise,所以我想在超时的情况下拒绝Promise.而且我想将与UI相同的值传递给UI,以防万一我收到来自服务的错误.基本上为空值,以便它稳定下来.但是在超时的情况下,UI不会收到任何值,并且我的屏幕加载程序会继续运行.

TIA.

解决方案

由于d3 v3不支持超时,因此您可以这样超时:

 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)
}) 

提供更高的超时工作代码此处

如果您使用d3.v4,则可以使用超时工作代码此处

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 so far but the UI gets hanged waiting for response. How can I specify a callback to reset the values and inform UI that no longer needed to wait.

q.promise(function(resolve, reject) {
  d3.xhr(my_url)
    .header('Content-Type', 'text/xml')
    .timeout(2000)
    .send(this.my_method, my_xmlData, function(error, data) {
    })
});

I read here that d3 xhr supports timeout feature now. How do I use that and execute a callback when request times out? Could anyone please tell me how to use that correctly? I tried using

.ontimeout(function(){console.log('request timedout!!'}) 

but it did not work.

I am using promises so I want to reject the promise in case of timeout. And I want to pass UI the same values it receives in case I receive error from service. Basically null values so that It settles down. But in case of timeout the UI does not receives any values and my screen loader keeps running.

TIA.

解决方案

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

这篇关于如何拒绝d3 xhr请求超时的承诺?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆