而与JavaScript循环

我正在探索node.jsasynchronous库来实现node.js中的游标( https://dev.twitter.com/docs/misc/cursoring )。

whilst看起来像我正在寻找的function,但我的情况有点不同。 每次我发出一个GET请求,我必须等待得到一个响应,然后更改光标值。

async文档中,这是给出的例子

 var count = 0; async.whilst( function () { return count < 5; }, function (callback) { count++; setTimeout(callback, 1000); }, function (err) { // 5 seconds have passed } ); 

我试图做一些像它的实现推特游标导航,但它似乎并没有工作:

 async.whilst( function(){return cursor != 0}, function(callback){ oa.get( 'https://api.twitter.com/1.1/friends/list.json?cursor=' + cursor + '&skip_status=true&include_user_entities=false' ,user.token //test user token ,user.tokenSecret //test user secret ,function (e, data, res){ if (e) console.error(e); console.log("I AM HERE"); cursor = JSON.parse(data).next_cursor; } ) }, function(){ console.log(cursor);//should print 0 } ) 

编辑:我的get请求callback中的console.log(“我在这里”)被调用一次,没有任何反应后..

我不认为在中间的function应该有一个callback,改变计数器, whilst只有在计数器在实际function而不是在其callback中改变时才有效。

async.whilst使用callback知道你的'worker'函数完成处理,所以记得总是调用callback参数, async.whilst传递给你的函数,作为它的第二个参数,当你准备好下一个循环'循环'。

我想什么是缺less的是从“进程”function的callback

就像是:

 async.whilst( function(){return cursor != 0}, function(callback){ oa.get( 'https://api.twitter.com/1.1/friends/list.json?cursor=' + cursor + '&skip_status=true&include_user_entities=false' ,user.token //test user token ,user.tokenSecret //test user secret ,function (e, data, res){ if (e) console.error(e); console.log("I AM HERE"); cursor = JSON.parse(data).next_cursor; callback(null, cursor ); } ) }, function(){ console.log(cursor);//should print 0 } ) 

注意到null作为第一个参数seance错误是一种标准。

希望这可以帮助别人。 问候