Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Getting 'responde finished' state as a callback #5

Open
newmandani opened this issue Mar 9, 2015 · 5 comments
Open

Getting 'responde finished' state as a callback #5

newmandani opened this issue Mar 9, 2015 · 5 comments

Comments

@newmandani
Copy link

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.

@RichardCzechowski
Copy link
Collaborator

I think there is only one page of categories. You may want to try this on the 'people' endpoint.

@newmandani
Copy link
Author

You are right, Richard. But is it a reason why "function (err, results)" fired before whole list loading?

@RichardCzechowski
Copy link
Collaborator

I think there is a problem in your callback. You are returning 'crunchCatsList' as the first argument in the callback (err) rather than the second (results).
Try changing this-
return cb(crunchCatsList);

to this-
cb(null, crunchCatsList);

@RichardCzechowski
Copy link
Collaborator

What is the exact output you are getting?

@newmandani
Copy link
Author

It's "undefined". You can see it's output here. server.1.js

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants