有望在超时的失败



我希望我的承诺如果未能在300毫秒内返回结果,如果失败,我希望要调用另一个承诺,该诺言将执行一个默认函数,该功能具有较小的执行时间。

我到目前为止尝试过的东西 -

    $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}
        });
}])

Service GetairMSG执行一个查询,该查询需要花费大量时间来花费几个值,查询目前已尽可能优化。因此,我希望有后备可用,仅返回基本信息。

现在面临的问题 - 控制台总是显示'时间到!对不起!数据:未定义'。该请求仍处于未返回该时间范围内值的情况下的待处理状态。

您可以使用timeout的CC_1设置。在此处查看示例用法。

作为您的代码的示例;

服务:

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!');
  }
});

显示控制台的原因是因为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)

相关内容

  • 没有找到相关文章

最新更新