$http 在单个请求中请求 return 所有没有分页的记录

$http request to return all records without pagination in a single request

有4000多条记录。 API returns 最多 1000 条记录并有分页。我调用这个函数(循环),并使用"skip"以1000条记录为间隔获取记录。

我一次需要所有记录,但下面的代码 returns 只需要前 1000 条记录。

  var array=[];
  function loop(skip){
     return $http({
            method: 'GET',
            url: myurl+'&$skip='+skip,
            timeout:5000
      }).success(function(res){
        if(res.d.length>0) 
            {
              Array.prototype.push.apply( array,res.d);
               loop(skip +1000);
            }
        return array;
      }).error(function(response,status,headers,config) {

      });
    }

    getAll = function() {
        return loop(0);
    }

我需要单个请求可以获取总记录。 但只有我得到这部分的前 1000 条记录:(

    getAll().then(function() {
        console.log("in this part i need the array with my 4000 records")
    });

应该可以通过一些递归承诺链接实现。试试这个

function getAll(page) {
    if (typeof page === 'undefined') {
        page = 0;
    }
    return $http.get(myurl, {$skip: page * 1000}).then(function(res) {
        var data = res.data;
        if (data.length > 0) {
            return getAll(page + 1).then(function(nextData) {
               return data.concat(nextData);
            });
        }
        return data;
    });
}

然后这样称呼它

getAll().then(function(allRecords) {
    console.log(allRecords);
});

这里有一个 Plunker 演示 ~ http://plnkr.co/edit/ey8gdytvuBE6cpuMAtnB?p=preview

首先,来自 angular doc 的一点旁注:

The $http legacy promise methods success and error have been deprecated. Use the standard then method instead. If $httpProvider.useLegacyPromiseExtensions is set to false then these methods will throw $http/legacy error.

另一种解决方案:将累加器结果作为参数传递给循环函数

function loop(skip,result){
  result = result||[];//accumulator for result, init empty array if not pass
  return $http(
    {
          method: 'GET',
          url: myurl+'&$skip='+skip,
          timeout:5000
    }).then(function success(response){
      if(response.data.length > 0){
        Array.prototype.push.apply(result,response.data);
        return loop(skip+1000,result);
      }
      return result;
    },function error(){

    });
}

注意,与当前代码的主要区别在于 return 在成功处理程序中调用 loop 函数之前。

这项工作,因为如果来自 then 函数 return 承诺,那么下一个 then 将在 returned 承诺实现后应用。

Sample plunkr

工作示例 https://jsfiddle.net/coolbhes/xmqur1bq/(用承诺替换服务)

适合您的代码如下:

var array = [];

function loop(skip, resolveHandle) {
    $http({
        method: 'GET',
        url: myurl + '&$skip=' + skip,
        timeout: 5000
    }).then(function successCallback(res) {
        if (res.d.length > 0) {
            Array.prototype.push.apply(array, res.d);
            loop(skip + 1000, resolveHandle);
        } else {
            //done with all requests;
            resolveHandle(array);
        }
    }, function errorCallback(rej) {

    });
};

function getAll() {
    return new Promise(function(resolve, reject) {
        loop(0, resolve);
    });
}

getAll().then(function(data) {
            console.log("in this part i need the array with my 4000 records", data)
        }