How to execute functions in parallel with async.js?

In the following code I have Array.forEach, it performs a synchronous function doSomethingin sequence:

items.forEach(function(item) {
doSomething(item);
});

I need to execute functions ( doSomething) in parallel, use async.jsand try the following:

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');
                    });

But the function was doSomethingperformed in sequence.

I tried with async.parallel, but the function doSomethingwas executed in sequence again:

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');
                    });

How to execute synchronous function doSomethingin parallel with async.js?

Please help me.

0
source share
4 answers

How to execute doSomething synchronous function in parallel with async.js?

. async.js , (. ). , JavaScript - ; "" .

, - (. JavaScript Long Running Tasks, Javascript ) WebWorkers . async.js .

+2

, Javascript - . , HTML5 , Web Workers, . HTML5, . Web Worker , , ().

+1

doSomething insto async, async.parallel, , .

0

You can only run these funcs (perhaps this is what you need), use eachSeries instead each. Here is a brief documentation for eachSeries:

The same as each iterator is applied to each element in the array sequentially. The next iterator is called only after the current one has finished processing. This means that the iterator functions will end in order.

Expression synchronous function in parallel- virtually absent. Parallel means performing asynchronization.

0
source

All Articles