Last active
August 29, 2015 14:16
-
-
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?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
bootleg that @mentions don't work on gists...
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
Can you make that
getData(resp)
internally have something as simple asI tried it, but it needs to be some deferred object because as it is it just pushes the promise into that array.