在下面的代码,我已经Array.forEach
,它执行序列中的doSomething
同步功能:如何与async.js并行执行函数?
items.forEach(function(item) {
doSomething(item);
});
我需要并行地执行的功能(doSomething
),使用async.js
和尝试以下方法:
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');
});
但函数doSomething
是按顺序执行的。
我曾试图与async.parallel
,但功能doSomething
在顺序再次执行:
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');
});
如何使用async.js
并行执行doSomething
同步功能?
请帮帮我。
感谢您的信息,后来我已经尝试与parallel.js,但我有问题与IE浏览器。 [Parallel.js在IE中有Blob问题](http:// stackoverflow。COM /问题/ 21744104 /并行JS-有-问题,与二进制大对象 - 在-IE) – kuskunko