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