简体   繁体   English

使用 Graph API 分页的 while 循环中的异步方法

[英]Asynchronous method in while loop with Graph API paged

I'm using facebook node sdk for node.js to get information from a facebook user such as their feed and friends, which is working fine.我正在使用facebook node sdk for node.js 从 facebook 用户那里获取信息,例如他们的提要和朋友,这工作正常。

However I'm having an issue where the returned data is paged - I need to build something in recursive mode.但是我遇到了一个问题,即返回的数据被分页 - 我需要在递归模式下构建一些东西。 Let me explain:让我解释:

FB.api('/me/feed?limit=5000', {
    access_token: token
}, function(response) {
  // response is an object that could have as response.paging.next attribute
}); 

Limit isn't working here, because it returns a max of 245 and returns paging object indicating the next page of results.限制在这里不起作用,因为它返回最大值 245 并返回指示下一页结果的分页对象。

Because the next call depends of the result of the previous async call, I tried to do something like this:因为下一个调用取决于上一个异步调用的结果,所以我尝试做这样的事情:

// first call before
var hasNext = response.paging.next ? true : false;
while (hasNext){
    FB.api('/me/feed', {
        access_token: token
    }, function(response_paged) {
       response.data.concat(response_paged.data);
       // If do not have a next page to break the loop
       if (!response_paged.paging.next) hasNext = false;
    });
} 

The way the next token was obtained is not important for now下一个令牌的获取方式目前并不重要

The point is I'm trying to do async calls in recursive mode, but its not working, this way I'm getting an infinite loop.关键是我试图在递归模式下进行异步调用,但它不起作用,这样我就得到了一个无限循环。

My idea of solving this with async/await: 我用async / await解决这个问题的想法:

async function getFeed(token) {
    let feedItems = [],
        hasNext = true,
        apiCall = '/me/feed';

    while (hasNext) {
        await new Promise(resolve => {
            FB.api(apiCall, {access_token: token}, (response) => {
                feedItems.concat(response.data);
                if (!response.paging.next) {
                    hasNext = false;
                } else {
                    apiCall = response.paging.next;
                }
                resolve();
            });
        });
    }
    return feedItems;
}

getFeed().then((response) => {
    console.log(response);
});

Be aware that you need Node.js 7.9.0+ for this: http://node.green/ 请注意,您需要为此使用Node.js 7.9.0+: http ://node.green/

For older versions, install this: https://github.com/yortus/asyncawait 对于旧版本,请安装以下版本: https : //github.com/yortus/asyncawait

You can also use a recursive function, but the smooth/modern way would be async/await. 您也可以使用递归函数,但是平滑/现代的方式是异步/等待。

Instead of using: 而不是使用:

feedItems.concat(response.data);

I have made: (if is the case on response.data has the data) 我已经做了:(如果在response.data上有数据的话)

for(var i in response.data){
   feedItems.push(response.data[i]);
}

As of today (12/10/21) response.data is of type Object.截至今天 (12/10/21) response.data 是 Object 类型。 The below would now be appropriate for saving response data.下面的内容现在适用于保存响应数据。

 for (let d of response.data) { feedItems.push(d) }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM