Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/39.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用承诺递归检索分页数据_Javascript_Node.js_Pagination_Promise_Bluebird - Fatal编程技术网

Javascript 使用承诺递归检索分页数据

Javascript 使用承诺递归检索分页数据,javascript,node.js,pagination,promise,bluebird,Javascript,Node.js,Pagination,Promise,Bluebird,我使用的函数以分页形式返回数据。因此,它将返回最多100个项目和一个键来检索接下来的100个项目。我想检索所有可用的项目 我如何递归地实现这一点?递归在这里是一个好的选择吗?如果没有递归,我可以用其他方法吗 我使用Bluebird 3x作为承诺库 以下是我试图实现的一个片段: getEndpoints(null, platformApplication) .then(function(allEndpoints) { // process on allEndpoints });

我使用的函数以分页形式返回数据。因此,它将返回最多100个项目和一个键来检索接下来的100个项目。我想检索所有可用的项目

我如何递归地实现这一点?递归在这里是一个好的选择吗?如果没有递归,我可以用其他方法吗

我使用Bluebird 3x作为承诺库

以下是我试图实现的一个片段:

getEndpoints(null, platformApplication)
  .then(function(allEndpoints) {
    // process on allEndpoints
  });


function getEndpoints(nextToken, platformApplication) {
  var params = {
    PlatformApplicationArn: platformApplication
  };

  if (nextToken) {
    params.NextToken = nextToken;
  }

  return sns.listEndpointsByPlatformApplicationAsync(params)
    .then(function(data) {
      if (data.NextToken) {
        // There is more data available that I want to retrieve.
        // But the problem here is that getEndpoints return a promise
        // and not the array. How do I chain this here so that 
        // in the end I get an array of all the endpoints concatenated.
        var moreEndpoints = getEndpoints(data.NextToken, platformApplication);
        moreEndpoints.push.apply(data.Endpoints, moreEndpoints);
      }

      return data.Endpoints;
    });
}

但问题是,如果要检索更多的数据(请参见
if(data.NextToken){…}
),如何将承诺链接起来,以便最终获得所有端点的列表等。

递归可能是获取所有端点的最简单方法

function getAllEndpoints(platformApplication) {
    return getEndpoints(null, platformApplication);
}

function getEndpoints(nextToken, platformApplication, endpoints = []) {
  var params = {
    PlatformApplicationArn: platformApplication
  };

  if (nextToken) {
    params.NextToken = nextToken;
  }

  return sns.listEndpointsByPlatformApplicationAsync(params)
    .then(function(data) {
      endpoints.push.apply(endpoints, data.Endpoints);
      if (data.NextToken) {
          return getEndpoints(data.NextToken, platformApplication, endpoints);
      } else {
          return endpoints;
      }
    });
}

你想什么时候拿到下一个100分?什么触发了这一点?当有更多可用数据时。如果data.NextToken可用,这意味着有更多数据可用,因此我必须获取它。那么您正在尝试获取所有端点?是的。抱歉,问题中不清楚,已更新。我明白了。默认参数,
endpoints=[]
是ES5还是ES6功能?它在Node.js版本0.10.36下工作吗?如果您没有访问默认参数(ES6)的权限,那么只需从
getAllEndpoints
传入一个空数组,或者手动检查
getEndpoints
中是否有未定义的数据。顺便说一句,推送不应该是这样的:
endpoints.push.apply(endpoints,data.endpoints)端点
,而不是
数据。端点
。我只是想确认一下……是的。我是从问题中抄来的。修好了,哦,对不起。我不知道push.apply是如何工作的,所以这就是我出错的原因,但现在我检查了文档并修复了它。谢谢你的帮助。它工作得很好。