如何在node.js中通过API回调异步递归
How to recurse asynchronously over API callbacks in node.js?
API调用返回结果的下一页。如何优雅地递归调用结果?
这里有一个我需要做这件事的例子:
var url = 'https://graph.facebook.com/me/?fields=posts&since=' + moment(postFromDate).format('YYYY-MM-DD') + '&access_token=' + User.accessToken;
request.get({
url: url,
json: true
}, function (error, response, body) {
if (!error && response.statusCode == 200) {
_.each(body.posts.data, function (post) {
User.posts.push(post); //push some result
});
if (body.pagination.next) { // if set, this is the next URL to query
//?????????
}
} else {
console.log(error);
throw error;
}
});
我建议将调用封装在一个函数中,并在必要时继续调用它。
我还想添加一个回调来了解流程何时完成。
function getFacebookData(url, callback) {
request.get({
url: url,
json: true
}, function (error, response, body) {
if (!error && response.statusCode == 200) {
_.each(body.posts.data, function (post) {
User.posts.push(post); //push some result
});
if (body.pagination.next) { // if set, this is the next URL to query
getFacebookData(body.pagination.next, callback);
} else {
callback(); //Call when we are finished
}
} else {
console.log(error);
throw error;
}
});
}
var url = 'https://graph.facebook.com/me/?fields=posts&since=' +
moment(postFromDate).format('YYYY-MM-DD') + '&access_token=' + User.accessToken;
getFacebookData(url, function () {
console.log('We are done');
});
相关文章:
- MeteorJS:在带有回调的vzaar api上正确使用wrapAsync
- 回调函数中传递参数的困难(Google Map API Markers)
- 如何通过bxSlider回调api处理事件
- Google Data API上的OOP Javascript回调方法
- JavaScript回调函数和Google Feed API
- 谷歌放置了api回调解决方法
- 如何在node.js中通过API回调异步递归
- 承诺API回调-如何正确解决或拒绝
- 如何将自定义回调javascript参数添加到bing API回调中
- 获取API回调的参数
- 从iTunes搜索API回调返回值
- 返回NULL值的Google Geocode API回调函数
- 节点API回调与服务器websocket通信
- 如何发送JQuery实例方法作为Google API回调
- IE7和IE8的YouTube API回调
- Angularjs:我可以让我的指令在控制器API回调后运行吗?
- SoundCloud SDK/neneneba API回调事件未启动
- AngularJS在API回调后恢复表单
- 获取 API 回调以在 ReactJS 中的函数外部传递
- json-in-script"的Youtube API回调直通