如何处理取决于其他多个异步函数异步函数 [英] How to handle an async function that depends on multiple other async functions
问题描述
我有跟另外两个异步函数的结果工作异步函数。
到现在为止我在做什么,是我写的回调功能1和功能2功能2在函数2的回调
功能1(callbackFunction参数(){
函数2(callbackFunction参数(){
function3()
})
})
是否有任何其他的方式来处理这个问题。我用JavaScript code在客户端,并在平时的NodeJS工作。
我的情况是,对于函数2我不需要从功能1输出。换言之功能1和功能2是独立的;但function3是依赖于功能1和功能2。
我希望我的函数2运行独立于功能1,但function3运行依赖于两个functio1和函数2。
有什么样
功能1();
函数2();
当(funtion1.complete&安培;&安培; funtion2.complete){
function3();
}
有一些好的库,以应对策划异步函数。 异步
和①
(或其他承诺/ A库)的帮助。
如果函数2
不依赖于功能1
的结果,可以并行执行它们。下面是它的外观采用异步
(这些例子假设你的回调有一个功能(错了,结果)
签名,这是节点的事实上的模式:
async.parallel([
函数(回调){功能1(回调); },
函数(回调){函数2(回调); }
],功能(ERR,价值观){
function3(值[0],值[1]);
});
如果函数2
取决于从功能1
,瀑布$ C $结果C>可能是更好的方式:
async.waterfall([
函数(回调){功能1(回调); },
功能(结果,回调){函数2(结果,回调); },
功能(结果,回调){function3(结果,回调); },
]);
我个人喜欢①
,因为你可以通过周围的承诺,并做各种漂亮的东西。这里是如何做到这一点看起来使用的:
q.nfcall(功能1)
。然后(功能(结果){返回q.nfcall(函数2);})
。然后(功能(结果){返回q.nfcall(function3);})
.fail(功能(错误){
//如果其中任何一个失败,这就是所谓的。
});
或者,如果功能1
和函数2
可以任意顺序进行:
q.all([q.nfcall(功能1),q.nfcall(函数2)])
。然后(功能(值){function3(值[0],价值[1]);})
.fail(功能(错误){
});
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
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.
My scenario is that for function2 I don't need output from function1. In another words function1 and function2 are independent; but function3 is dependent on function1 and function2.
I want my function2 to run independent on function1 but function3 to run dependent on both functio1 and 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.
If function2
does not depend on the result of function1
, you can execute them in parallel. 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.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:
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. 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:
q.all([q.nfcall(function1), q.nfcall(function2)])
.then(function(values) { function3(values[0], values[1]); })
.fail(function(err) {
});
这篇关于如何处理取决于其他多个异步函数异步函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!