Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9106325
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T02:18:45+00:00 2026-06-17T02:18:45+00:00

I am new to javascript and node.js and this is my first post, so

  • 0

I am new to javascript and node.js and this is my first post, so please bear with me.

I am using ntwitter to get all previous tweets of a specific user.

My problem is that if the user has more than 200 tweets, I need to create a loop and I am not sure if I do it right.

This is the async function that gets the 200 latest tweets:

exports.getUserTimeline = function(user, callback) {

  twit.getUserTimeline({ screen_name: user, count: 200 }, function(err, data) {
    if (err) { 
      return callback(err);
    }
    callback(err, data);  
  });
}

I found a solution to do this using a recursive function, but it’s quite ugly.. How can I improve it ?

exports.getUserHistory = function(user, callback) {
  recursiveSearch(user, callback);
  function recursiveSearch(user, callback, lastId, data) {
    var data = data || []
      , args = {screen_name: user, count: 200};

    if(typeof lastId != "undefined") args.max_id = lastId;

    twit.getUserTimeline(args, function(err, subdata) {
      if (err) { 
        console.log('Twitter search failed!');
        return callback(err);
      }
      if (data.length !== 0) subdata.shift();
      data = data.concat(subdata);
      var lastId = parseInt(data[data.length-1].id_str);
      if (subdata.length !== 0) {
        recursiveSearch(user, callback, lastId, data);
      } else {
        callback(err, data);
      }
    });
  }
}

Thank’s a lot!

Update: This is the improved (refactored) function suggested by hunterloftis with two modifications:

  1. property max_id should not be specified on the first iteration
  2. the case where the user exists but no tweets have been posted must be handled

code:

function getUserHistory(user, done) {
  var data = [];
  search();

  function search(lastId) {
    var args = {
      screen_name: user,
      count: 200,
      include_rts: 1
    };
    if(lastId) args.max_id = lastId;

    twit.getUserTimeline(args, onTimeline);

    function onTimeline(err, chunk) {
      if (err) {
        console.log('Twitter search failed!');
        return done(err);
      }

      if (!chunk.length) {
        console.log('User has not tweeted yet');
        return done(err);
      }

      //Get rid of the first element of each iteration (not the first time)
      if (data.length) chunk.shift();

      data = data.concat(chunk);
      var thisId = parseInt(data[data.length - 1].id_str);

      if (chunk.length) return search(thisId);
      console.log(data.length + ' tweets imported');
      return done(undefined, data);
    }
  }
}

When retrieving tweets I noticed that my tweet count wasn’t always the same as the ‘statuses_count’ property of the user. It took me some time to figure out that this difference corresponds to the number of deleted tweets 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T02:18:46+00:00Added an answer on June 17, 2026 at 2:18 am

    Does your recursive function work? Doesn’t look too bad to me. I might refactor it just a little into something more like this:

    function getUserHistory(user, done) {
      var data = [];
      search();
    
      function search(lastId) {
        var args = {
          screen_name: user,
          count: 200,
          max_id: lastId
        };
    
        twit.getUserTimeline(args, onTimeline);
    
        function onTimeline(err, chunk) {
          if (err) {
            console.log('Twitter search failed!');
            return done(err);
          }
    
          if (data.length) chunk.shift(); // What is this for?
          data = data.concat(chunk);
          var thisId = parseInt(data[data.length - 1].id_str);
    
          if (chunk.length) return search(thisId);
          return done(undefined, data);
        }
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Please keep in mind that I am VERY new to all of this javascript/node.js
All, I'm pretty new with JavaScript and this is my first attempt with the
im new at javascript and i can get this slider to work but not
I'm using Nodejs and Socket.io. When the client connects, new JavaScript objects are created.
New to javascript and such, trying to create a loop for fading logos using
New to javascript, but I'm sure this is easy. Unfortunately, most of the google
Ok im new to javascript and Unity so this is clearly a very noob
I am new to javascript and node, and have been trying to create an
I'm new in JavaScript, so excuse me if this question is not worth asking.
Sorry, still reasonably new to Javascript here, so hope this question isn't too embarrassingly

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.