取消 pendingRequests 不适用于 angularJS 1.7.2 版本

Cancelling pendingRequests is not working for angularJS 1.7.2 version

当再次调用相同的 api 时,我正在尝试取消挂起的 api 调用。我有一个下拉菜单 onChange,我正在对它进行 api 调用,这里 api 调用需要花费很多时间来处理 return 数据,所以我取消了 pendingRequest 数组中的所有先前调用并保持只有最近触发的呼叫。请通过下面的代码

在我的控制器中

angular.forEach($http.pendingRequests,function(r){
        console.log(r.contractId);
        if (r.contractId && r.contractId !== selectedContract) {
           r.cancel.resolve('cancelled');
        }
 });

在这里,我通过比较当前选定的合约与 pendingRequest 数组来检查待处理请求是否最新。

这就是我编写服务的方式

getDashboardData:function(selectedContract) {
    var deferred = $q.defer();
    var cancel  = $q.defer();
    $rootScope.loaderOn=true;
    var url = 'my_url_here';
  $http.get(url, {
        ignoreLoadingBar: true,
        headers: {
            'Content-Type': 'application/json',
            'Accept': 'application/json'
        },
        cancel:cancel, //this is of type $q.defer();
        contractId:selectedContract // here i assigned contrract id just to delete all pending api's other than current
    }).then(function(data){
        deferred.resolve(data.data);
            $rootScope.loaderOn=false;      
    },function(error){
          console.log(error);
          return deferred.reject(err);
      });

    return deferred.promise;
}

我想取消或拒绝挂起的 api 来电。任何帮助将不胜感激谢谢。让我知道是否需要任何其他信息。请在这里指正。

const request = new Map() // put this outside the main function

getDashboardData() { // call this on change
  const url = 'my_url_here';  // value should be constant
  const oldRequest = request.get(url) // get the last promise request 

  oldRequest.reject && oldRequest.reject('old request') // cancel's the last promise request
  // last promise request successCallback won't be called since the promise is already rejected

  $q((resolve, reject) => { // creates a new promise
    request.set(url, { resolve, reject }) // store resolve, reject
    return $http.get(url) // create api request
  })
  .then(successCallback) // on request success
  .catch(rejectedOrErrorCallaback)
}

您可以将其重新实现为类似这样的东西。 顺便说一句,根据文档。 $http.pendingRequests 主要用于调试目的。所以我认为这是一个远离使用

的标志

我提到了 $http 并且怀疑取消 属性 是否存在于 $http.get 函数的配置对象中。您可以使用超时 属性 并使用该值来取消现有请求。

我已将取消 属性 替换为超时。

希望以下代码对您有所帮助。

// Declare the below objects in service level not inside getDashboardData function 
var deferredObj = '';
var existingRequest = '';

getDashboardData: function(selectedContract) {
var deferred = $q.defer();

// Below condition will check to cancel the existing request
if (existingRequest) {
   existingRequest = '';
   deferredObj.resolve();
   deferredObj = '';
} else {
    deferredObj = $q.defer();
}
$rootScope.loaderOn=true;
var url = 'my_url_here';
existingRequest = $http.get(url, {
    ignoreLoadingBar: true,
    headers: {
        'Content-Type': 'application/json',
        'Accept': 'application/json'
    },
    **timeout : deferredObj.promise**, //this is of type $q.defer();
    contractId:selectedContract // here i assigned contrract id just to delete all pending api's other than current
}).then(function(data){
    deferred.resolve(data.data);
        $rootScope.loaderOn=false;      
},function(error){
      console.log(error);
      return deferred.reject(err);
  });

return deferred.promise;
}