Saturday, 27 May 2017

Recursive asynchronous api calls

I'm working on an project that requires me to use the twitter api. Trying to return all the followers of a given user. The problem is getting all the user bc twitter separate followers into blocks of at most 200.

I saw a solution online that they call the callback recursively, however I'm getting stuck trying to use promises to get all the data back before proceeding. Currently it waits for the first page to come back but idk how to make it wait for all the promises to return. Can someone give me any hints/tips and if theres a way to do this iteratively?

let cursor = -1;
let promise = new Promise((resolve,reject)=>{
      twitter.followers({cursor},function callback(data, error){
       if(error)
           reject(false)
      cursor = data.next_cursor;
      if(cursor!=0){
           new Promise((resolve,reject)=>{
                twitter.followers({cursor},callback)
           })
           resolve(true);
      }
})
})
promise.then({
    //do stuff
})



via Leon

No comments:

Post a Comment