Angular 承诺超时失败
Angular promise failure on timeout
我希望我的承诺在 300 毫秒内无法 return 结果时失败,如果失败,我希望调用另一个承诺,该承诺将执行执行时间更短的默认函数。
到目前为止我尝试过的 -
$timeout(function(){
GetAirMsg.get({mDate: $filter('date')($scope.realdateFrom,'ddMMyy'),
mDateO: $filter('date')($scope.realdateFrom,'yyyyMMdd'),
mDateD: $filter('date')($scope.realdateFrom,'dd-MMM-yyyy'),
acode: $filter('uppercase')($scope.air),
offset: offsetInt}).$promise.then(function(result){
/* result evaluation */
});
},300).then(function(data){
console.log('Timed out! sorry!');
/*execute another promise with shorter execution time */
console.log('data : '+data);
},function(error){
//console.log('Timed out! sorry!');
});
服务是 -
angular.module('example').factory('GetAirMsg',['$resource',function($resource){
return $resource(serverBaseUrl + '/getAirMsg/:mDate/:mDateO/:mDateD/:acode/:offset',
{mDate: '@mDate',mDateO: '@mDateO',mDateD: '@mDateD',acode: '@acode',offset: '@offset'}, {
get: {method: 'GET', isArray: false}
});
}])
服务 GetAirMsg 执行一个查询,该查询为几个值占用了大量时间,该查询目前已尽可能优化。因此,我希望可以使用 return 仅提供基本信息的后备方案。
现在面临的问题 - 控制台总是显示
'时间到!对不起!
数据:未定义'。对于在该时间范围内没有 return 值的案例,请求将保持待处理状态。
您可以使用 $resource
的 timeout
设置。查看示例用法 here.
作为您的代码示例;
服务:
angular.module('example').factory('GetAirMsg',['$resource',function($resource){
return $resource(serverBaseUrl + '/getAirMsg/:mDate/:mDateO/:mDateD/:acode/:offset',
{mDate: '@mDate',mDateO: '@mDateO',mDateD: '@mDateD',acode: '@acode',offset: '@offset'}, {
get: {method: 'GET', isArray: false, timeout: 300}
});
}])
控制器:
GetAirMsg.get({mDate: $filter('date')($scope.realdateFrom,'ddMMyy'),
mDateO: $filter('date')($scope.realdateFrom,'yyyyMMdd'),
mDateD: $filter('date')($scope.realdateFrom,'dd-MMM-yyyy'),
acode: $filter('uppercase')($scope.air),
offset: offsetInt}).$promise
.then(function(result){
/* result evaluation */
})
.catch(function() {
console.log("error!");
});
显示该控制台的原因是因为 timeout
函数成功运行并且由于 timeout
没有返回任何数据,data
将是 undefined
试试这个。
控制器代码:
$scope.isFirstCallSuccess = false;
$timeout(function(){
GetAirMsg.get({
mDate: $filter('date')($scope.realdateFrom,'ddMMyy'),
mDateO: $filter('date')($scope.realdateFrom,'yyyyMMdd'),
mDateD: $filter('date')($scope.realdateFrom,'dd-MMM-yyyy'),
acode: $filter('uppercase')($scope.air),
offset: offsetInt})
.$promise
.then(function(result){
/* result evaluation */
$scope.isFirstCallSuccess = true; // updating only if call success
}, ,function(error){
// API call was failure even before the timeout
});
},300).then(function(){
if(!$scope.isFirstCallSuccess){
//the call was failure. the value is still false
/*execute another promise with shorter execution time */
console.log('Timed out! sorry!');
}
});
编辑:
方法:再创建一个服务来取消承诺。如果在超时结束前未完成,请使用该服务取消
服务:
function cancel( promise ) {
//it will cancel the promise only the promise is still active
if (promise) {
promise.reject();
}
}
控制器:
$scope.gettingAirMsg = GetAirMsg.get({
mDate: $filter('date')($scope.realdateFrom,'ddMMyy'),
mDateO: $filter('date')($scope.realdateFrom,'yyyyMMdd'),
mDateD: $filter('date')($scope.realdateFrom,'dd-MMM-yyyy'),
acode: $filter('uppercase')($scope.air),
offset: offsetInt})
.$promise
.then(function(result){
//success
}, ,function(error){
});
$timeout(function(){
yourService.cancel($scope.gettingAirMsg)
//it will cancel the promise only the promise is still active
// if the promise ends before 300ms the cancel call will not pass active promise
},300)
显示该控制台的原因是因为 timeout
函数成功运行并且由于 timeout
没有返回任何数据,data
将是 undefined
试试这个。
控制器代码:
$scope.isFirstCallSuccess = false;
$timeout(function(){
GetAirMsg.get({
mDate: $filter('date')($scope.realdateFrom,'ddMMyy'),
mDateO: $filter('date')($scope.realdateFrom,'yyyyMMdd'),
mDateD: $filter('date')($scope.realdateFrom,'dd-MMM-yyyy'),
acode: $filter('uppercase')($scope.air),
offset: offsetInt})
.$promise
.then(function(result){
/* result evaluation */
$scope.isFirstCallSuccess = true; // updating only if call success
},function(error){
// API call was failure even before the timeout
});
},300).then(function(){
if(!$scope.isFirstCallSuccess){
//the call was failure. the value is still false
/*execute another promise with shorter execution time */
console.log('Timed out! sorry!');
}
});
我希望我的承诺在 300 毫秒内无法 return 结果时失败,如果失败,我希望调用另一个承诺,该承诺将执行执行时间更短的默认函数。
到目前为止我尝试过的 -
$timeout(function(){
GetAirMsg.get({mDate: $filter('date')($scope.realdateFrom,'ddMMyy'),
mDateO: $filter('date')($scope.realdateFrom,'yyyyMMdd'),
mDateD: $filter('date')($scope.realdateFrom,'dd-MMM-yyyy'),
acode: $filter('uppercase')($scope.air),
offset: offsetInt}).$promise.then(function(result){
/* result evaluation */
});
},300).then(function(data){
console.log('Timed out! sorry!');
/*execute another promise with shorter execution time */
console.log('data : '+data);
},function(error){
//console.log('Timed out! sorry!');
});
服务是 -
angular.module('example').factory('GetAirMsg',['$resource',function($resource){
return $resource(serverBaseUrl + '/getAirMsg/:mDate/:mDateO/:mDateD/:acode/:offset',
{mDate: '@mDate',mDateO: '@mDateO',mDateD: '@mDateD',acode: '@acode',offset: '@offset'}, {
get: {method: 'GET', isArray: false}
});
}])
服务 GetAirMsg 执行一个查询,该查询为几个值占用了大量时间,该查询目前已尽可能优化。因此,我希望可以使用 return 仅提供基本信息的后备方案。
现在面临的问题 - 控制台总是显示 '时间到!对不起! 数据:未定义'。对于在该时间范围内没有 return 值的案例,请求将保持待处理状态。
您可以使用 $resource
的 timeout
设置。查看示例用法 here.
作为您的代码示例;
服务:
angular.module('example').factory('GetAirMsg',['$resource',function($resource){
return $resource(serverBaseUrl + '/getAirMsg/:mDate/:mDateO/:mDateD/:acode/:offset',
{mDate: '@mDate',mDateO: '@mDateO',mDateD: '@mDateD',acode: '@acode',offset: '@offset'}, {
get: {method: 'GET', isArray: false, timeout: 300}
});
}])
控制器:
GetAirMsg.get({mDate: $filter('date')($scope.realdateFrom,'ddMMyy'),
mDateO: $filter('date')($scope.realdateFrom,'yyyyMMdd'),
mDateD: $filter('date')($scope.realdateFrom,'dd-MMM-yyyy'),
acode: $filter('uppercase')($scope.air),
offset: offsetInt}).$promise
.then(function(result){
/* result evaluation */
})
.catch(function() {
console.log("error!");
});
显示该控制台的原因是因为 timeout
函数成功运行并且由于 timeout
没有返回任何数据,data
将是 undefined
试试这个。
控制器代码:
$scope.isFirstCallSuccess = false;
$timeout(function(){
GetAirMsg.get({
mDate: $filter('date')($scope.realdateFrom,'ddMMyy'),
mDateO: $filter('date')($scope.realdateFrom,'yyyyMMdd'),
mDateD: $filter('date')($scope.realdateFrom,'dd-MMM-yyyy'),
acode: $filter('uppercase')($scope.air),
offset: offsetInt})
.$promise
.then(function(result){
/* result evaluation */
$scope.isFirstCallSuccess = true; // updating only if call success
}, ,function(error){
// API call was failure even before the timeout
});
},300).then(function(){
if(!$scope.isFirstCallSuccess){
//the call was failure. the value is still false
/*execute another promise with shorter execution time */
console.log('Timed out! sorry!');
}
});
编辑:
方法:再创建一个服务来取消承诺。如果在超时结束前未完成,请使用该服务取消
服务:
function cancel( promise ) {
//it will cancel the promise only the promise is still active
if (promise) {
promise.reject();
}
}
控制器:
$scope.gettingAirMsg = GetAirMsg.get({
mDate: $filter('date')($scope.realdateFrom,'ddMMyy'),
mDateO: $filter('date')($scope.realdateFrom,'yyyyMMdd'),
mDateD: $filter('date')($scope.realdateFrom,'dd-MMM-yyyy'),
acode: $filter('uppercase')($scope.air),
offset: offsetInt})
.$promise
.then(function(result){
//success
}, ,function(error){
});
$timeout(function(){
yourService.cancel($scope.gettingAirMsg)
//it will cancel the promise only the promise is still active
// if the promise ends before 300ms the cancel call will not pass active promise
},300)
显示该控制台的原因是因为 timeout
函数成功运行并且由于 timeout
没有返回任何数据,data
将是 undefined
试试这个。
控制器代码:
$scope.isFirstCallSuccess = false;
$timeout(function(){
GetAirMsg.get({
mDate: $filter('date')($scope.realdateFrom,'ddMMyy'),
mDateO: $filter('date')($scope.realdateFrom,'yyyyMMdd'),
mDateD: $filter('date')($scope.realdateFrom,'dd-MMM-yyyy'),
acode: $filter('uppercase')($scope.air),
offset: offsetInt})
.$promise
.then(function(result){
/* result evaluation */
$scope.isFirstCallSuccess = true; // updating only if call success
},function(error){
// API call was failure even before the timeout
});
},300).then(function(){
if(!$scope.isFirstCallSuccess){
//the call was failure. the value is still false
/*execute another promise with shorter execution time */
console.log('Timed out! sorry!');
}
});