简体   繁体   English

AngularJS $ q。所有超时

[英]AngularJS $q.all timeout

I'm using AngularJS's $q function to group together several promises. 我正在使用AngularJS的$ q函数将几个promise组合在一起。 However, I also want to set a timeout to cancel this promise and do something else if there isn't any response for any of these promises after a certain time. 但是,我还想设置一个超时时间来取消此承诺,并且如果在一定时间后对这些承诺中的任何一个都没有任何响应,请执行其他操作。 Is this possible? 这可能吗?

Code: 码:

var data = {};
var map_dataFile = $http.get("GET Request 1"),
    node_dataFile = $http.get("GET Request 2"),
    country_data = $http.get("GET Request 3"),
    shipment_data = $http.get("GET Request 4"),
    location_data = $http.get("GET Request 5");
$q.all([map_dataFile, node_dataFile, country_data, shipment_data, location_data]).then(function(results) {
    data.countries = results[0].data;
    data.nodes = results[1].data;
    data.countries_indicator = results[2].data;
    data.shipment_flows = results[3].data;
    data.location_data = results[4].data;
});
//do something else if it takes more than 10s, and cancel the $q.all promise

$http and the get method can take a config object. $httpget方法可以使用config对象。 One of the supporting parameters on this config object is timeout . 此配置对象的支持参数之一是timeout See documentation 参阅文件

You can pass a timeout value to http config and if any of the operation would take more time the complete $q promise would get rejected. 您可以将超时值传递给http config,如果任何操作花费更多时间,则完整的$q诺言将被拒绝。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM