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

add another async.parallel example #114

Closed
mariusa opened this issue Mar 13, 2012 · 6 comments
Closed

add another async.parallel example #114

mariusa opened this issue Mar 13, 2012 · 6 comments

Comments

@mariusa
Copy link

mariusa commented Mar 13, 2012

Hi,

Would it be possible please to add an example to the docs showing how to use async.parallel with a real-life use?
I simply don't get it how it should work from docs, from test/ or from
http://stackoverflow.com/questions/7236899/nodejs-async-same-arguments-to-an-array-of-functions

A nice example would be to read 2 files and make a db call (all these 3 in parallel, async), and then do something with all 3.

Here's a start, which I can't tell how to make it work:
https://gist.github.com/2030967

Thanks

@rlidwka
Copy link

rlidwka commented Mar 13, 2012

Hm... usage here is the same as in other functions basically.

Ok, that's real-world example, hope it helps:

var mongo;
var httpserv;

async.parallel([
    function(callback) {
        var mongodb = require('mongodb');
        mongo = new mongodb.Db('project', new mongodb.Server("127.0.0.1", 27017, {auto_reconnect: true}));
        mongo.open(callback);
    },

    function(callback) {
        httpserv = require('http').createServer(function (request, response) {
            process_request(request, response);
        });
        httpserv.listen(1337, callback);
    },
], function() {
    Application.init(mongo, httpserv);
});

PS: variables should be return values of course... but you've asked about real life - how it is, not how it should be :D

@ianwalter
Copy link

Mariusa, I re-wrote your code example here: https://gist.github.com/4535307
async.parallel uses callbacks to populate the results array or object.
Someone should close this issue.

@brianmaissy
Copy link
Contributor

Anyone else feel that the example in the docs isn't clear? @mariusa do you feel that this example adds something substantial that wasn't communicated by that example? If so, we should add it to the docs. If not, @caolan should close this.

@caolan
Copy link
Owner

caolan commented Feb 6, 2013

I'd like the examples to be concise and clearly show the concept. I don't want them to teach JavaScript or Node.js. If people feel the underlying concept isn't described by the example then we should update it, otherwise I'll close this.

@brianmaissy
Copy link
Contributor

Judging by the initial (incorrect) example proposal by @rlidwka the misunderstanding was more an issue of higher-order functions than the usage of async.parallel. I think the example in the docs is clear.

@caolan
Copy link
Owner

caolan commented Feb 6, 2013

Agreed, closing.

@caolan caolan closed this as completed Feb 6, 2013
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

5 participants