Skip to content

Instantly share code, notes, and snippets.

@amykyta
Last active August 29, 2015 14:16
Show Gist options
  • Save amykyta/a764a03c3d08362799c2 to your computer and use it in GitHub Desktop.
Save amykyta/a764a03c3d08362799c2 to your computer and use it in GitHub Desktop.
What's a nicer way to do this? Without having that all_data array around?
var qhttp = require('q-io/http');
var _ = require('lodash');
var some_url = "http://localhost:8080/getsome/"
var makeNextUrl = _.bind(String.prototype.concat, some_url);
var all_data = [];
function getData(resp){
resp = JSON.parse(resp);
if(resp.nextLink){
all_data.push(resp.data)
return qhttp.read(makeNextUrl(resp.nextLink))
.then(getData);
} else {
return all_data.concat(resp.data).join('');
}
}
qhttp.read(makeNextUrl('0'))
.then(getData)
.then(console.log)
.done()
@amykyta
Copy link
Author

amykyta commented Mar 5, 2015

Can you make that getData(resp) internally have something as simple as

if(resp.nextLink){
   return [].concat(\* call for the next one *\)

I tried it, but it needs to be some deferred object because as it is it just pushes the promise into that array.

@amykyta
Copy link
Author

amykyta commented Mar 5, 2015

bootleg that @mentions don't work on gists...

@griffinmyers
Copy link

I would do this recursively. Recursion + async is a tasty combo.

I haven't run this, but consider something like this:

function get_all(initial) {
  return ajax(initial).then(function(result) {
    if(result.next) {
      return get_all(result.next).then(function(inner_result) {
        inner_result.push(result.data);
        return inner_result;
      });
    }
    else {
      return [result.data]
    }
  }); 
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment