Skip to content

Instantly share code, notes, and snippets.

Last active December 20, 2015 21:18
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
Recursion + Promises by the brilliant @nhunzaker
function harvest(id, parent) {
var promise = $.Deferred();
var post;
url: "...",
json: true,
timeout: 30000
}, function(error, response, body) {
if (error) return console.log("CRAP");
// Enter the pseudocode....
post = JSON.parse(body);
post.children = [];
// Now assign this post to a parent if it was specified
if (parent) {
// Now for each reblog (or whatever it's called) do it again,
// which will assign a parent id, but also this will return
// a promise that we'll use to determine if children are finished
var promises = {
// Go out and get the reply, which will assign the
// post's id as the parent attribute
return harvest(reply, post);
$.when.apply($, promises).then(function() {
return promise;
var posts;
harvest("your seed id").done(function(data) {
posts = data;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment