简体   繁体   English

等待Node.js中的多个回调

[英]Waiting for multiple callbacks in Node.js

I have a Node.js application where multiple funcions might be called, depending on several factors, but only one last function is called after the last callback. 我有一个Node.js应用程序,可能会调用多个函数,具体取决于几个因素,但在最后一次回调后只调用一个最后一个函数。

This is a simplified version of what I got: 这是我得到的简化版本:

if(foo === bar){
    function1(arg1, function(val1){
        doWhatever(val1, function(){
            res.end("Finished");
        });
    });
}else if(foo === baz){
    function2(arg2, function(val2){ 
        doWhatever(val2, function(){
            res.end("Finished");
        });
    });
}else{
    function3(arg3, function(val3){
        doWhatever(val3, function(){
            res.end("Finished");
        });
    });
}

And this is what im doing: 这就是我正在做的事情:

var finished = false;

if(foo === bar){
    function1(arg1, function(val1){
        result = val1;
        finished = true;
    });
}else if(foo === baz){
    function2(arg2, function(val2){ 
        result = val2;
        finished = true;
    });
}else{
    function3(arg3, function(val3){
        result = val3;
        finished = true;
    });
}

var id = setInterval(function(){
    if(finished === true){
        clearInterval(id);
        doWhatever(result, function(){
            res.end("Finished");
        });
    }
}, 100);

I guess this can be simplified by using promises, however im not sure how should I implement them. 我想这可以通过使用promises来简化,但是我不确定应该如何实现它们。

You could also do it using when and promises, which IMHO is the easiest to read. 你也可以使用时间和承诺来做,恕我直言是最容易阅读的。

var promises = [];

if(x) {
    var deferred1 = when.defer();
    doSomethingAsync({ callback: deferred1.resolve });
    promises.push(deferred1.promise);
} else if(y) {
    var deferred2 = when.defer();
    doSomethingAsync({ callback: deferred2.resolve });
    promises.push(deferred2.promise);
} else if(z) {
    var deferred3 = when.defer();
    doSomethingAsync({ callback: deferred3.resolve });
    promises.push(deferred3.promise);
}

when.all(promises).then(function () {
    console.log('Finished Promises');
});

Here's one way with async series. 这是async系列的一种方式。

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

async.series([
    function(callback){
        if(foo === bar){
            function1(arg1, function(val1){
                callback(null, val1);
            });
        }else if(foo === baz){
            function2(arg2, function(val2){ 
                callback(null, val2);
            });
        }else{
            function3(arg3, function(val3){ 
                callback(null, val3);
            });
        }
    }  
], function(error, valArray){
       doWhatever(valArray[0], function(){
           res.end("Finished");
       });
});

Here's using wait.for 这是使用wait.for

https://github.com/luciotato/waitfor https://github.com/luciotato/waitfor

//in a fiber
var result;
if(foo === bar){
    result = wait.for(function1,arg1);
}else if(foo === baz){
    result = wait.for(function2,arg2);
}else{
    result = wait.for(function3,arg3);
};

doWhatever(result, function(){
        res.end("Finished");
});

You need to be in a fiber (or generator) to use wait.for, but, if you have a lot of callback hell, wait.for is a good approach. 你需要在光纤(或生成器)中使用wait.for,但是,如果你有很多回调地狱,wait.for是一个很好的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM