add another async.parallel example #114

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

Comments

Projects
None yet
5 participants

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

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.

Contributor

brianmaissy commented Feb 6, 2013

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.

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.

Contributor

brianmaissy commented Feb 6, 2013

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.

Owner

caolan commented Feb 6, 2013

Agreed, closing.

@caolan caolan closed this Feb 6, 2013

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