Skip to content

Instantly share code, notes, and snippets.

@lucnap
Last active May 4, 2016 08:55
Show Gist options
  • Save lucnap/9b61e8702d16e2d7f3d0cb5fd3604caf to your computer and use it in GitHub Desktop.
Save lucnap/9b61e8702d16e2d7f3d0cb5fd3604caf to your computer and use it in GitHub Desktop.
Javascript Nodejs recursive async call function for data collection from a remote source
/*
the example find data from facebook api search endpoint
*/
var findPages = function($center, $limit, $offset, callback) {
FB.api("/search", {q: "", type: "place", center: $center, distance: "15000", limit: $limit, offset: $offset, access_token: access_token}, function(result) {
callback(result);
});
};
var getLocationData = function(icenter, ilimit, ioffset, callMebackWhenFinish) {
var allPagesFoundInLocation = [];
// only for the first call
getNextDataPage(icenter, ilimit, ioffset);
function getNextDataPage(center, limit, offset) {
findPages(center, limit, offset, function(res) {
res.data.forEach(function(row) {
allPagesFoundInLocation.push(row);
});
if (res.data.length < limit) {
callMebackWhenFinish(allPagesFoundInLocation);
} else {
getNextDataPage(center, limit, offset + limit);
}
});
}
};
var center = "40.75,-73.98";
var limit = 100;
getLocationData(center, limit, 0, function(result) {
console.log("all work done");
console.log(result);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment