admin管理员组文章数量:1294326
I am trying to write the Pseudocode given here with javascript using 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. 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(
'.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);
}
);
}
}
I am trying to write the Pseudocode given here https://dev.twitter./docs/misc/cursoring with javascript using node-oauth https://github./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. 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./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);
}
);
}
}
Share
edited Aug 9, 2013 at 23:17
Alberto Zaccagni
31.6k11 gold badges75 silver badges107 bronze badges
asked Aug 9, 2013 at 23:04
s_curry_ss_curry_s
3,4329 gold badges33 silver badges49 bronze badges
1
- 1 looking into this right now.. geekabyte.blogspot./2013/04/… – s_curry_s Commented Aug 9, 2013 at 23:12
2 Answers
Reset to default 6Suppose your code is wrapped in a function, I'll call it getFriends
, basically it wraps everything inside the loop.
function getFriends(cursor, callback) {
var url = 'https://api.twitter./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:
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.
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.
本文标签: nodejsWhile loop in Javascript with a CallbackStack Overflow
版权声明:本文标题:node.js - While loop in Javascript with a Callback - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741587380a2386920.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论