gpt4 book ai didi

jquery - 等待多个延迟函数调用完成的问题

转载 作者:太空宇宙 更新时间:2023-11-04 02:42:41 24 4
gpt4 key购买 nike

我正在尝试使用 node-facebook-sdk使用 Node.JS 进行 FB 图形 API 调用

我想从所有用户 friend 那里获取提要数据。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 没有那么丰富的经验。

我想我的问题的一个更简单的形式是:我如何等待多个回调函数完成?

非常感谢。

最佳答案

编辑:封装的回调计数递减:http://jsfiddle.net/3L9zc/32/

function BatchAPICalls(){
var calls = [];
var self = this;

self.push = function(){
var args = [];

for(var i = 1; i < arguments.length - 1; i++)
args.push(arguments[i]);

calls.push({
fn: arguments[0],
callback: arguments[arguments.length - 1],
args: args
});
};

self.start = function(callback){
var callbacksLeft = calls.length;

function batchCallback(){
callbacksLeft--;
if(callbacksLeft == 0)
callback();
}

for(var i = 0; i < calls.length; i++){

var call = calls[i];

var batchItemCallback = function(){
call.callback.apply(this, arguments);
batchCallback();
};

call.fn.apply(this, call.args.concat([batchItemCallback]));
}
};
}



var numFriends = friends.data.length;
var batch = [];
var feed_dump = [];

for (var i = 0, ii = numFriends / 50; i < ii; i++) {
batch.push([]);
}

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'
});
}

function done() {
console.log('hopefully feed_dump has been updated...');
PySocket.emit('update_user_graph',JSON.stringify(feed_dump));
}

var batchCalls = new BatchAPICalls();

for (var i in batch) {
var bucket = batch[i];

batchCalls.push(FB.api, 'call #' + i, 'post', {'batch' : bucket}, function(res){

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.');

batchCalls.start(done);

关于jquery - 等待多个延迟函数调用完成的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12433642/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com