Skip to content Skip to sidebar Skip to footer

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.facebo

Solution 1:

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');
});

Post a Comment for "How To Recurse Asynchronously Over API Callbacks In Node.js?"