Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to recurse asynchronously over API callbacks in node.js?

An API call returns the next 'page' of results. How do I recurse over that result callback elegantly?

Here is an example of where I need to do this:

var url = 'https://graph.facebook.com/me/?fields=posts&since=' + moment(postFromDate).format('YYYY-MM-DD') + '&access_token=' + User.accessToken;
request.get({
    url: url,
    json: true
}, function (error, response, body) {
    if (!error && response.statusCode == 200) {
        _.each(body.posts.data, function (post) {
            User.posts.push(post); //push some result
        });
        if (body.pagination.next) { // if set, this is the next URL to query
            //?????????
        }
    } else {
        console.log(error);
        throw error;
    }

});
like image 868
metalaureate Avatar asked Jun 21 '13 19:06

metalaureate


1 Answers

I would suggest wrapping the call in a function and just keep calling it until necessary.

I would also add a callback to know when the process has finished.

function getFacebookData(url, callback) {

    request.get({
        url: url,
        json: true
    }, function (error, response, body) {
        if (!error && response.statusCode == 200) {
            _.each(body.posts.data, function (post) {
                User.posts.push(post); //push some result
            });
            if (body.pagination.next) { // if set, this is the next URL to query
                getFacebookData(body.pagination.next, callback);
            } else {
                callback(); //Call when we are finished
            }
        } else {
            console.log(error);
            throw error;
        }

    });
}

var url = 'https://graph.facebook.com/me/?fields=posts&since=' + 
    moment(postFromDate).format('YYYY-MM-DD') + '&access_token=' + User.accessToken;

getFacebookData(url, function () {
    console.log('We are done');
});
like image 110
DeadAlready Avatar answered Sep 30 '22 20:09

DeadAlready