最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Parallel function calls in Node.js - Stack Overflow

programmeradmin0浏览0评论

I need to do a few independent database queries in Node.js. After all queries are executed, response should be sent. My first try looks like this:

templateData = {};

model.getA(function(result) {
    templateData.A = result;

    model.getB(function(result) {
        templateData.B = result;

        model.getC(function(result) {
            templateData.C = result;

            response.send('template', templateData);
        })
    })
});

Of course, this approach in Node.js is not good at all, because all functions are called sequentially and I'm loosing advantages of asynchronous programming pattern. I'm new to Node.js and it's still unclear to me how to call getA(), getB() and getC() in parallel and send response just after everything is finished. Is there some really simple and common way to achieve this?

I need to do a few independent database queries in Node.js. After all queries are executed, response should be sent. My first try looks like this:

templateData = {};

model.getA(function(result) {
    templateData.A = result;

    model.getB(function(result) {
        templateData.B = result;

        model.getC(function(result) {
            templateData.C = result;

            response.send('template', templateData);
        })
    })
});

Of course, this approach in Node.js is not good at all, because all functions are called sequentially and I'm loosing advantages of asynchronous programming pattern. I'm new to Node.js and it's still unclear to me how to call getA(), getB() and getC() in parallel and send response just after everything is finished. Is there some really simple and common way to achieve this?

Share Improve this question asked Dec 26, 2012 at 0:46 Peter KrejciPeter Krejci 3,1926 gold badges33 silver badges49 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 14

Use the async package: (npm install async)

async.parallel([
    function(){ ... },
    function(){ ... }
], callback);

https://github.com/caolan/async#parallel

Alternatively, you can use promises:

Q.spread(
    [ model.getA(), model.getB(), model.getC() ],
    function(a, b, c) {
        // set templateData
        return templateData;
    }
).then(...);

(assuming that the get*() methods return promises)

You could just use a function that checks if all the data is present as your callback to your queries, and if all the data is present then the response could be sent. So something like

function checkData(){
    if (templateData.A && templateData.B && templateData.C){
        //send your response
    }
}

then just don't nest your calls

model.getA(function(result){
    templateData.A = result;
    checkData();
}

model.getB(function(result){
    templateData.B = result;
    checkData();
}

model.getC(function(result){
    templateData.C = result;
    checkData();
}

When all three have completed your response will be sent.

发布评论

评论列表(0)

  1. 暂无评论