0

So I'm using codebird-js to interact with the twitter API in JavaScript, but the limit on tweets the api fetches at a certain time is 100. Because of this, I need to make consecutive calls to the API but change the "max_id" parameter to shift the timeframe and get the next 10 tweets.

My problem is that I'm storing tweets directed at a specific account in an array because those are the only ones I care about, but the code moves on before the array is fully filled in a for loop. I have tried to use promises and callbacks, but I couldn't get either to work the way I intended. My most recent attempt was to use jquery's when/then, but it still gives the same results as my other attempts. Below is some code with an attempt I made where I am trying to just print the first element of the array, which always comes up undefined. I know that the array is being created though, because if I just print the array the JS console shows it filled. Thank you for any advice you may have!

actualReplies = []
    var params = {
        to:"someTwitterHandle",
        count:100,
        result_type:"popular"
    }

    $.when(
        cb.__call(
        "search_tweets",
        params,
        function (reply){
            for(var i=0; i<reply.statuses.length; i++){
                if(reply.statuses[i].in_reply_to_status_id_str == tweetId){
                    actualReplies.push(reply.statuses[i])
                }
            }
            recent = reply.statuses[reply.statuses.length-1].id_str
        }
    )).then( function(){
        console.log(actualReplies[0])
    })
codeitup
  • 55
  • 8
  • 2
    It's because you're still using a callback for `cb.__call`. If I'm not mistaken, the `when/then` doesn't know to wait until the callback is finished (unless, of course, `cb.__call` returns a `deferred` or a `promise`) – user184994 Dec 10 '17 at 19:35
  • Wow, that was exactly what it was. Thank you so much, can't believe it was that easy. – codeitup Dec 10 '17 at 20:13

0 Answers0