I want to call async.parellel([],function(){})
inside a for loop. When i tried the for loop completes execution before the async function execution.How to resolve this problem.
我想在for循环中调用async.parellel([],function(){})。当我尝试for循环完成执行异步函数执行之前。如何解决此问题。
2 个解决方案
#1
0
You cannot solve the problem, for it is not a problem, it is how async functions work.
你无法解决问题,因为它不是问题,它是异步函数的工作方式。
If you start a bunch of async functions during the loop, the latter executes as a whole and puts the formers on the loop to be executed once it releases the control of the flow.
如果在循环期间启动一堆异步函数,后者将作为一个整体执行,并且一旦释放流控制就将执行器放在循环上执行。
Because of that, you'll see probably the function containing the loop terminates before the async functions start.
因此,您可能会看到包含循环的函数在异步函数启动之前终止。
The usual approach to give a response to the caller once all the async functions have terminated is by using a callback function as a parameter of the one that contains the root, thus nicely handle the async ones and invoke the callback once all of them have returned.
所有异步函数终止后,通常使用回调函数作为包含根的参数,从而很好地处理异步函数并在所有函数返回后调用回调函数。 。
#2
0
I agree with Yelizaveta Gagarina.
我同意Yelizaveta Gagarina的观点。
You can do some stuff like that if you want to work with your for loop:
如果你想使用for循环,你可以做一些这样的事情:
var fns = [];
for (var i = 0; i < 9; i++) {
fns.push(function(callback){
// do stuff here and call your callback with or without error
callback(null, stuff); // no err in this example
});
}
async.parallel(fns, function(err, stuff){
if(err) return cb(err); // in this example err is null
// do stuff, call next middleware if you are in this case
next();
});
There is also async.series if you want to execute some functions 1 per 1. Hope this helps you.
如果你想每1执行一些函数,也有async.series。希望这对你有所帮助。
#1
0
You cannot solve the problem, for it is not a problem, it is how async functions work.
你无法解决问题,因为它不是问题,它是异步函数的工作方式。
If you start a bunch of async functions during the loop, the latter executes as a whole and puts the formers on the loop to be executed once it releases the control of the flow.
如果在循环期间启动一堆异步函数,后者将作为一个整体执行,并且一旦释放流控制就将执行器放在循环上执行。
Because of that, you'll see probably the function containing the loop terminates before the async functions start.
因此,您可能会看到包含循环的函数在异步函数启动之前终止。
The usual approach to give a response to the caller once all the async functions have terminated is by using a callback function as a parameter of the one that contains the root, thus nicely handle the async ones and invoke the callback once all of them have returned.
所有异步函数终止后,通常使用回调函数作为包含根的参数,从而很好地处理异步函数并在所有函数返回后调用回调函数。 。
#2
0
I agree with Yelizaveta Gagarina.
我同意Yelizaveta Gagarina的观点。
You can do some stuff like that if you want to work with your for loop:
如果你想使用for循环,你可以做一些这样的事情:
var fns = [];
for (var i = 0; i < 9; i++) {
fns.push(function(callback){
// do stuff here and call your callback with or without error
callback(null, stuff); // no err in this example
});
}
async.parallel(fns, function(err, stuff){
if(err) return cb(err); // in this example err is null
// do stuff, call next middleware if you are in this case
next();
});
There is also async.series if you want to execute some functions 1 per 1. Hope this helps you.
如果你想每1执行一些函数,也有async.series。希望这对你有所帮助。