简体   繁体   English

如何处理依赖于多个其他异步函数的异步函数

[英]How to handle an async function that depends on multiple other async functions

I have an asynchronous function that works with the result of two other async functions. 我有一个异步函数,可以处理另外两个异步函数的结果。

Till now what I was doing is that I write function2 in the callback function1 and function2 in the callback of function2 直到现在我正在做的是我在回调函数1中编写function2,在function2的回调中编写函数2

function1(callbackFunction() {
    function2(callbackFunction() {
        function3()
    })
})

Is there any other way to handle this. 有没有其他办法来处理这个问题。 I work usually with JavaScript code in client side and in nodeJs. 我通常使用客户端和nodeJs中的JavaScript代码。

My scenario is that for function2 I don't need output from function1. 我的情况是,对于function2,我不需要function1的输出。 In another words function1 and function2 are independent; 换句话说,function1和function2是独立的; but function3 is dependent on function1 and function2. 但是function3依赖于function1和function2。

I want my function2 to run independent on function1 but function3 to run dependent on both functio1 and function2. 我希望我的function2在function1上独立运行,但是function3依赖于functio1和function2来运行。

Is there anything like 有没有像

function1();
function2();
when(funtion1.complete && funtion2.complete) {
    function3();
}

There are some good libraries to deal with orchestrating asynchronous functions. 有一些很好的库来处理编排异步函数。 async and q (or other Promises/A libraries) help. asyncq (或其他Promises / A库)有帮助。

If function2 does not depend on the result of function1 , you can execute them in parallel. 如果function2不依赖于function1的结果,则可以并行执行它们。 Here's how it looks using async (these examples assume that your callback has a function(err, result) signature, which is the defacto pattern for Node: 以下是使用async外观(这些示例假设您的回调具有function(err, result)签名,这是Node的实际模式:

async.parallel([
    function(callback) { function1(callback); },
    function(callback) { function2(callback); }
], function(err, values) {
    function3(values[0], values[1]);
});

If function2 depends on the result from function1 , waterfall may be a better pattern: 如果function2取决于从结果function1waterfall可能是一个更好的模式:

async.waterfall([
    function(callback) { function1(callback); },
    function(result, callback) { function2(result, callback); },
    function(result, callback) { function3(result, callback); },
]);

Personally, I like q , because you can pass promises around and do all kinds of nifty stuff. 就个人而言,我喜欢q ,因为你可以通过承诺并做各种漂亮的东西。 Here's how this would look using that: 以下是使用它的方式:

q.nfcall(function1)
  .then(function(result) { return q.nfcall(function2); })
  .then(function(result) { return q.nfcall(function3); })
  .fail(function(err) {
      // If any of them fail, this is called.
  });

Or if function1 and function2 can be done in arbitrary order: 或者,如果function1function2可以按任意顺序完成:

q.all([q.nfcall(function1), q.nfcall(function2)])
 .then(function(values) { function3(values[0], values[1]); })
 .fail(function(err) {
 });

Here is a solution that I baked. 这是我烘焙的解决方案。 You can try a call manager to call the dependant functions 您可以尝试使用呼叫管理器来调用相关功能

var func1 = function() {
    console.log("Dependant call ... " + 1);
};
var func2 = function() {
    console.log("Dependant call ... " + 2);
};
var func3 = function() {
    console.log("Dependant call ... " + 3);
};
var func4 = function() {
    console.log("Dependant call ... " + 4);
};


var CallManager = function(funcs_config) {
    var _this = this;

    _this.functions = funcs_config;
    _this.callAsynchronous = function(f) {
        if (f != undefined) {
            for (var i = 0; i < f.length; i++) {
                f[i].call(function() {
                    this.callAsynchronous(f.splice(0,1));
                });
            }
            return;
        }

        for (var func in _this.functions) {
            if (_this.functions[func].length == 0) {
                //not dependent to any function
            } else {
                console.log('Calling....' + func);
                _this.callAsynchronous(_this.functions[func]);
                eval(func +'();');
            }
        }
    };

    return _this;
};


var callManager = new CallManager({
    //dependency configuration
    func2: [func1], //func2 depends on func1
    func3: [func2],
    func4: [func1, func3] //func4 depends on both func1 and func3
});

callManager.callAsynchronous();

With the current config provided above, when it is run the output is given as - 使用上面提供的当前配置,当它运行时,输出为 -

Calling....func2
Dependant call ... 1
Dependant call ... 2
Calling....func3
Dependant call ... 2
Dependant call ... 3
Calling....func4
Dependant call ... 1
Dependant call ... 3
Dependant call ... 4

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

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