Skip to content

Getting 'responde finished' state as a callback #5

Description

@newmandani

As NodeJS and this particular API is async I'm trying to make fetching procedure 'end' state but have no experience enough and can't find a right way. Can you show me the right way to do so with current API?

async.parallel({
    readCrunch: function(cb, results) {
        console.log('\r\nStart Crunchbase Categories fetching...');
        crunchCatsLoad({page: 1}, crunchCatsList);
        return cb(crunchCatsList);
    }
}, function (err, results) {
    if(results) {
        console.log(results,' crunchbase Categories fetched.');
        console.log('Why you are "undefined"?');
    } 
    if (err) {
        console.log('ERRR: ', err);
    }
});

var crunchCatsLoad = function(query, arrayReturn) {
    var catsArray = [];
    crunchbase.categories( { page: query.page }, function(error, cats) {

        if (cats) {
            var items = cats.data.items;
            var next_page_url = cats.data.paging.next_page_url;

            items.forEach( function(category, i) {
                catsArray.push({
                // some code
                });
            });

            if ( next_page_url !== null ) {
                query.page++;
                console.log('Getting page ', query.page);
                crunchCatsLoad({ page: query.page }, arrayReturn);
            } else {
                arrayReturn = catsArray;
                console.log(catsArray.length, ' Crunchbase cats fetched.');
                return arrayReturn;
            }
        }
    });
};

Hope to hear an advice or best practice from you.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions