AngularJS $resource - X 毫秒后调用回退 URL
AngularJS $resource - call fallback URL after X milliseconds
我希望我的前端应用程序能够切换到另一个 api 以防第一个出现故障
例如:我改为调用 https://api.example.com/users?name=bob - then I get net::ERR_CONNECTION_TIMED_OUT
(a Chrome XHR response), which indicates that the api is non-responsive. I now would like my front-end to call https://api1.example.com/users?name=bob。
我查看了 AngularJS 1.5.7 $resource 的文档,其中指出它采用 {number}
类型的操作参数 timeout
。但是,将其设置为例如 500 仍然会在大约 2 分钟后抛出 net::ERR_CONNECTION_TIMED_OUT
!
通缉流量:
- 问https://api.example.com/users?name=bob
- 如果这在 10 秒内没有回答:
- 在我还有后端的时候继续问 apiX
伪代码:
angular.forEach(fallback_urls, function(url) {
$resource(url + '/users?name=bob', {}, {timeout: 10}).get()
});
您已声明您对 $resource 的操作是错误的。
$resource(url + '/users?name=bob', {}, {
'get': {
method: 'GET',
timeout: 10000
}
});
您可能还想查看 "cancellable" 选项。
var res = $resource(url + '/users?name=bob', {}, {
'get': {
method: 'GET',
cancellable: true
}
});
var response = res.get();
var timeoutPromise = $timeout(function(){
response.$cancelRequest();
},10000);
response.$promise.then(function(){
$timeout.cancel(timeoutPromise);
});
然后以某种方式循环您声明的备份 $resource 对象。如果呼叫解决,当然会中止超时!希望这能给你一些线索!
我希望我的前端应用程序能够切换到另一个 api 以防第一个出现故障
例如:我改为调用 https://api.example.com/users?name=bob - then I get net::ERR_CONNECTION_TIMED_OUT
(a Chrome XHR response), which indicates that the api is non-responsive. I now would like my front-end to call https://api1.example.com/users?name=bob。
我查看了 AngularJS 1.5.7 $resource 的文档,其中指出它采用 {number}
类型的操作参数 timeout
。但是,将其设置为例如 500 仍然会在大约 2 分钟后抛出 net::ERR_CONNECTION_TIMED_OUT
!
通缉流量:
- 问https://api.example.com/users?name=bob
- 如果这在 10 秒内没有回答:
- 在我还有后端的时候继续问 apiX
伪代码:
angular.forEach(fallback_urls, function(url) {
$resource(url + '/users?name=bob', {}, {timeout: 10}).get()
});
您已声明您对 $resource 的操作是错误的。
$resource(url + '/users?name=bob', {}, {
'get': {
method: 'GET',
timeout: 10000
}
});
您可能还想查看 "cancellable" 选项。
var res = $resource(url + '/users?name=bob', {}, {
'get': {
method: 'GET',
cancellable: true
}
});
var response = res.get();
var timeoutPromise = $timeout(function(){
response.$cancelRequest();
},10000);
response.$promise.then(function(){
$timeout.cancel(timeoutPromise);
});
然后以某种方式循环您声明的备份 $resource 对象。如果呼叫解决,当然会中止超时!希望这能给你一些线索!