title: While loop in Javascript with a Callback
body: <p>I am trying to write the Pseudocode given here <a href="https://dev.twitter.com/docs/misc/cursoring" rel="nofollow">https://dev.twitter.com/docs/misc/cursoring</a> with javascript using  node-oauth <a href="https://github.com/ciaranj/node-oauth" rel="nofollow">https://github.com/ciaranj/node-oauth</a>. 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. Can anyone think a workaround for this? </p>
  
  <pre><code>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 + '&amp;skip_status=true&amp;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);   
          }
        );
      }  
    }
  </code></pre>