1

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途中の関数には、カウンターを変更するコールバックがあり、コールバックではなく実際の関数でカウンターが変更された場合にのみ機能するとは思わない..

4

2 に答える 2

1

async.whilstはコールバックを使用して「ワーカー」関数の処理がいつ終了したかを認識します。そのため、「ループ」の次のサイクルの準備ができたら、関数に渡すパラメーターを 2 番目callbackのパラメーターとして常に呼び出すことを忘れないでください。async.whilst

于 2013-08-12T16:54:12.657 に答える
0

欠けているのは、「プロセス」関数内からのコールバックだと思います

何かのようなもの:

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

最初のパラメーター seance err は一種の標準であるため、null に注意してください。

これが誰かを助けることを願っています。よろしく

于 2014-12-24T23:36:07.073 に答える