Como executar funções em paralelo com async.js?
No código a seguir, eu tenhoArray.forEach
, Ele executa odoSomething
função síncrona em sequência:
items.forEach(function(item) {
doSomething(item);
});
Eu preciso executar funções (doSomething
) em paralelo, useasync.js
e tente o seguinte:
async.each(items, function (item, doneCallback) {
var startDate = new Date();
console.log(item.name().toString() + ' startDate: ' + startDate.toString() + ' - ' + startDate.getMilliseconds().toString());
doSomething(item); //Lazy function for many operations.
var endDate = new Date();
console.log(item.name().toString() + ' endDate' + endDate.toString() + ' - ' + endDate.getMilliseconds().toString());
return doneCallback(null);
}, function (err) {
otherFunction();
console.log('Finished');
});
Mas funçãodoSomething
foi executado em sequência.
Eu tentei comasync.parallel
, mas funçãodoSomething
foi executado em sequência novamente:
items.forEach(function (item) {
var func = function (doneCallback) {
var startDate = new Date();
console.log(item.name().toString() + ' startDate: ' + startDate.toString() + ' - ' + startDate.getMilliseconds().toString());
doSomething(item); //Lazy function for many operations.
var endDate = new Date();
console.log(item.name().toString() + ' endDate' + endDate.toString() + ' - ' + endDate.getMilliseconds().toString());
return doneCallback(null);
};
functions.push(func);
});
async.parallel(functions, function (err, results) {
otherFunction();
console.log('Finished');
});
Como executardoSomething
função síncrona em paralelo comasync.js
?
Por favor me ajude.