0
var url = posts.paging.next;
for (var i = 0; i < 3; i++) {
    (function(url,i) {
request(url,{json:true}, function (error, response, body) {
  if (!error && response.statusCode == 200) {
  url = body.paging.next;
  console.log(i+"+++"+url)
  }
});
    })(url,i);
};//for

According to async request response (body.paging.next), İ want to change value of var url which is top of code .Please Help

NOTE: i'm trying to get all comments from facebook api , To do that , i need to get page links . Because of this , i wrote this codes , if you have any other alternative way please suggest them thanx

1 Answer 1

1

Assuming that you're trying to perform the requests in sequence (e.g. the next request relies on the url from the previous request), you could do something like (using the async module):

var async = require('async');

// ...

var url = posts.paging.next, i = 0;

async.whilst(
  function() { return i < 3; },
  function(callback) {
    ++i;
    request(url, { json: true }, function(err, response, body) {
      // TODO: handle `err`
      //return callback(err);

      if (!err && response.statusCode === 200) {
        url = body.paging.next;
        console.log(i + '+++' + url);
        callback();
      }
    });
  },
  function(err) {
    // done!
  }
);
Sign up to request clarification or add additional context in comments.

1 Comment

this is correct answer , thank you so much , i was using async module differently some times , at this time you and async saved my time and i got some node.js experiance point :D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.