[英]Node.js Recursive Reddit API Calls
I'd like to get a list of all users' flairs on a particular subreddit. 我想获取特定subreddit上所有用户的天赋列表。 Reddit splits the requests up into chunks of 1,000 and allows a "before" and "after" parameter so that all of these can be fetched. Reddit将请求分为1,000个大块,并允许使用“ before”和“ after”参数,以便可以提取所有这些参数。 However, I can't get my head around how to create a recursive function to return an object containing everything. 但是,我无法理解如何创建递归函数以返回包含所有内容的对象。
An example request would be something like: 请求示例如下所示:
GET http://www.reddit.com/r/soccer/api/flairlist.json?limit=1000&after=t2_83wp8
The returned response would be a JSON object, with an array of users and a "prev" and "next" string which can be placed into the "before" and "after url parameters respectively. 返回的响应将是一个JSON对象,带有一个用户数组以及一个“ prev”和“ next”字符串,可以分别将其放入“ before”和“ after url”参数中。
It's something like this: 就像这样:
function getEverything(callback) {
var results = [];
function getSomeMore(callback, after) {
var url = 'http://...?limit=1000' + (after ? 'after=' + encodeURIComponent(after) : '');
makeAjaxRequest(url, function(err, body) {
if (err) return cb(err);
results = results.concat(body.results);
if (body.after)
getSomeMore(callback, body.after);
else
callback(null, results);
});
}
makeAjaxRequest(callback);
}
... and use it something like this: ...并使用如下所示的内容:
getEverything(function (err, everything) {
if (err) return console.log('ERROR: cant get everything:', err);
console.log('everything:', everything);
});
Though this is obviously pseudo-code only. 虽然这显然只是伪代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.