node.js でカーソル ( https://dev.twitter.com/docs/misc/cursoring )を実装するために node.js 非同期ライブラリを調査しています。
whilst
私が探している機能のように見えますが、私の場合はもう少し異なります。要求を行うたびにGET
、応答を待ってからカーソル値を変更する必要があります。
ドキュメントでは、これasync
は次の例ですwhilst
var count = 0;
async.whilst(
function () { return count < 5; },
function (callback) {
count++;
setTimeout(callback, 1000);
},
function (err) {
// 5 seconds have passed
}
);
Twitterのカーソルナビゲーションを実装するためにそのようなことを試みましたが、うまくいかないようです:
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 request コールバックの console.log("I AM HERE") は一度だけ呼び出され、その後は何も起こりません..
whilst
途中の関数には、カウンターを変更するコールバックがあり、コールバックではなく実際の関数でカウンターが変更された場合にのみ機能するとは思わない..