简体   繁体   English

在Javascript中循环使用回调

[英]While loop in Javascript with a Callback

I am trying to write the Pseudocode given here https://dev.twitter.com/docs/misc/cursoring with javascript using node-oauth https://github.com/ciaranj/node-oauth . 我试图写在这里给出的伪代码https://dev.twitter.com/docs/misc/cursoring使用节点的OAuth使用javascript https://github.com/ciaranj/node-oauth However I am afraid because of the nature of the callback functions the cursor is never assigned to the next_cursor and the loop just runs forever. 但是我担心由于回调函数的性质,游标永远不会分配给next_cursor,循环只会永远运行。 Can anyone think a workaround for this? 谁能想到解决这个问题?

module.exports.getFriends = function (user ,oa ,cb){
  var friendsObject = {};
  var cursor = -1 ;
  while(cursor != 0){
    console.log(cursor);
      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);
          cursor = JSON.parse(data).next_cursor;
          JSON.parse(data).users.forEach(function(user){
            var name = user.name;
            friendsObject[name + ""] = {twitterHandle : "@" + user.name, profilePic: user.profile_image_url};
          });        
          console.log(friendsObject);   
        }
      );
    }  
  }

Suppose your code is wrapped in a function, I'll call it getFriends , basically it wraps everything inside the loop. 假设你的代码包含在一个函数中,我将其称为getFriends ,基本上它将所有内容包装在循环中。

function getFriends(cursor, callback) {
  var url = 'https://api.twitter.com/1.1/friends/list.json?cursor=' + cursor + '&skip_status=true&include_user_entities=false'
  oa.get(url, user.token, user.tokenSecret, function (e, data, res) {
    if (e) console.error(e);
    cursor = JSON.parse(data).next_cursor;
    JSON.parse(data).users.forEach(function(user){
      var name = user.name;
      friendsObject[name + ""] = {twitterHandle : "@" + user.name, profilePic: user.profile_image_url};
    });        
    console.log(friendsObject);
    callback(cursor); 
  });
}

In nodejs all io is done asynchronously, so you will loop a lot more than needed, before actually changing cursor , what you need is loop only when you receive a response from the Twitter API, you could do something like this: 在nodejs中,所有io都是异步完成的,因此在实际更改cursor之前,你将循环比需要多得多,只有当你从Twitter API收到响应时才需要循环,你可以这样做:

function loop(cursor) {
  getFriends(cursor, function(cursor) {
    if (cursor != 0) loop(cursor);
    else return;
  });
}

You start it by calling loop(-1) , of course this is just one way of doing it. 你通过调用loop(-1)启动它,当然这只是一种方法。

If you prefer you could use an external library, like async . 如果您愿意,可以使用外部库,如异步

I strongly suggest using async for this. 我强烈建议使用异步 It's made for situations like yours and handles concurrency and execution for you. 它适用于像您这样的情况,并为您处理并发和执行。 You will simply end up writing something that does the same thing as async, only yours will not be as tested. 你最终会写一些与async相同的东西,只有你的东西不会被测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM