【发布时间】:2012-09-19 02:45:36
【问题描述】:
我正在尝试使用 node-facebook-sdk 使用 Node.JS 进行 FB 图形 API 调用
我想从所有用户朋友那里获取提要数据。 FB graph API 每次批量请求只允许 50 个朋友,所以我正在做一系列批量 FB.api() 调用。
我正在尝试将 FB.api() 调用存储在一个数组中,然后使用 jQuery when() 调用延迟函数。
问题:
1) 我传递给 .done() 函数的函数过早执行。 2) console.log(feed) 在 .done() 之后执行,据我所知这不应该发生。
$ = require('jQuery');
//maximum batch size request is 50. use 2-D array to store in buckets of 50 friends each.
var numFriends = friends.data.length;
var batch = [];
var deferred = [];//array to hold all the requests
var feed_dump = [];//where we collect all the feed data
//initialize a bucket for each set of 50 friends
for (var i = 0, ii = numFriends / 50; i < ii; i++) {batch.push([]);}
//put corresponding requests in in buckets.
for (var i = 0; i < numFriends; i++) {
batch[Math.floor(i/50)].push({ method: 'get', relative_url: friends.data[i].id + '/feed?since=' + '-1 month'});//not sure if the date format will work user.last_updated
}
//make the multiquery request for each bucket
for (var i in batch) {
var bucket = batch[i];
//nested FB api call
deferred.push(FB.api('', 'post', {'batch' : bucket}, function(res){//res = array 50 friend feeds
if (!res || res.error) {
console.log(!res ? 'error occurred' : res.error); return;
}
for (var j in res) {
var feed = JSON.parse(res[j].body);
console.log(feed);
feed_dump.push(feed);//feed for each friend appeneded to dump
}
}));
}
console.log('this should show up before graph api requests are made.');
//jQuery when() function.
$.when.apply(null, deferred).done(function() {
console.log('hopefully feed_dump has been updated...');
PySocket.emit('update_user_graph',JSON.stringify(feed_dump));
});
如何正确推迟批处理 FB.api() 请求?另外,如果有人能想到更好的方法,请告诉我;我对异步 javascript 没有那么丰富的经验。
我想我的问题的一个更简单的形式是:如何等待多个回调函数完成?
非常感谢。
【问题讨论】:
-
你不应该把你推送的“延迟”内容包装在一个函数中,所以它只会被 $.when.apply 调用吗?
标签: jquery node.js facebook-graph-api