0

So, I am using the Twit package to use the Twitter API, and there is one function that I have that is meant to check for duplicate tweets:

function checkDuplicate(tweet){
  var isDuplicate = false;
  var params = {
    screen_name: "reddevilsbot",
    count: 1
  }
  T.get('statuses/user_timeline', params, function (err, data, response){
    for(var j = 0; j < 1; j++){

      console.log('data: ', data[j].text.substring(0, 10));
      console.log('tweet: ', tweet.substring(0,10));
      console.log(data[j].text.substring(0, 10) == tweet.substring(0,10));

      if(data[j].text.substring(0, 10) == tweet.substring(0,10)){
        console.log('Im here');
        isDuplicate = true;
        break;
      }
    }
  });
  return isDuplicate;
}

If you are not familiar the Twitter API, what I am doing here is calling my own twitter handle in the params variable, then I call a get for statuses under a specific user. I am then given a data object that would return exactly one tweet, in this scenario.

So, for the sake of testing, I am checking ONLY the most recent tweet (hence, why count is set to 1), and I am passing a tweet into checkDuplicate() that is identical to the one I am comparing to. I have five logs to the console. The first one is outside of the function, and it is logging the result of checkDuplicate(), the rest are shown above. The results of the console are shown below.

    false
    data:  "Antonio V
    tweet:  "Antonio V
    true
    Im here

Seeing this leaves me to conclude that the checkDuplicate() is returning isDuplicate before the get call is finished. Is this true? If so, how do I make sure that the checkDuplicate() is waiting for the get call to finish?

Thanks

Aman
  • 41
  • 7
  • 1
    because it is asynchronous. You call a pizza place, you hang up the phone, and you expect to eat your pizza. – epascarello Jul 01 '17 at 02:55
  • Wow, that was quick! That's what I was thinking. Thank you, I have a solution for this problem now that I know that! – Aman Jul 01 '17 at 02:57
  • I was going to answer this but it got closed... Heres what I came up with: https://gist.github.com/shaun-sweet/6d190bb58b475e967ebe67db3958beeb – Shaun Sweet Jul 01 '17 at 03:17
  • Thank you! The first comment, it looked like you were trying to give me a link to documentation. It didn't seem to go through. Could you comment it here so I could see it? Thanks – Aman Jul 01 '17 at 03:57

0 Answers0