Friday 21 April 2017

Getting all the twitter followers using 'followers/ids' with promises

Been trying to get all the twitter followers of a user by hitting the 'followers/ids' endpoint.
I'm kind of stuck on how to get this to work with a while loop and promises.

I've been reading the documentation here: Using cursors to navigate collections

What I came up with is something like this, but it only fetches once:

      let nextCursor = -1
      while (nextCursor !== 0) {
        return T.get('followers/ids', {cursor: nextCursor, count: 20})
          .then(response => {
            console.log(response.data)
            nextCursor = response.data.next_cursor
            console.log(nextCursor)
          })
          .catch(err => {
            console.log(err)
          })
          console.log(nextCursor)
      }

If I don't use return there I end up with an infinite loop as nextCursor is never getting modified to the new value. But of course using return only allows me to make one request.

Count is set to 20 just for development purposes, I'm using an account that doesn't have a huge number of followers.

Can anyone push me in the right direction here ?



via frankbash

No comments:

Post a Comment