node.js - While loop in Javascript with a Callback -


i trying write pseudocode given here https://dev.twitter.com/docs/misc/cursoring javascript using node-oauth https://github.com/ciaranj/node-oauth. afraid because of nature of callback functions cursor never assigned next_cursor , loop runs forever. can think workaround 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 code wrapped in function, i'll call getfriends, wraps inside loop.

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 io done asynchronously, loop lot more needed, before changing cursor, need loop when receive response twitter api, this:

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

you start calling loop(-1), of course 1 way of doing it.

if prefer use external library, async.


Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

Function that returns a formatted array in VBA -