简体   繁体   中英

AngularJS $q.all timeout

I'm using AngularJS's $q function to group together several promises. 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. One of the supporting parameters on this config object is 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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