1

i try to understand and use async library in node. What i am really not understand, how async.parallel function worked. The name parallel it seems for like multithreading, consider the following sample:

async.parallel([
    function(callback){
        setTimeout(function(){
            console.log('1');
            callback(null, 'one');
        }, 200);
    },
    function(callback){
        setTimeout(function(){
            console.log('2');
            callback(null, 'two');
        }, 100);
    }
],
// optional callback
function(err, results){
    if(err){
        console.log('Error');
    } else {
        console.log(results);
    }
    // the results array will equal ['one','two'] even though
    // the second function had a shorter timeout.
});

i have got the result

[ 'one', 'two' ]  

Do async.parallel execute on multithread? If not, what the name parallel express?

1 Answer 1

7

The placement of results within the array is gauranteed by your placement of those functions within the array you pass to parallel. Notice that functions you pass to parallel have to take the same arguments. Picture these functions being placed in a wrapper and then called like this

someArrayOfFunctions[index](arguments);

Using this method, async gaurantees that independent of when functions within parallel finishes, the results are placed in the array in the order expected, through use of callbacks for populating results, based on indices. The specific implementation does not matter, the fact is no matter when you finish, the results of respective runs will end up placed based on where their function was in the array, not based on timing.

As per your second question, whether async.parallel is truly parallel. It is not. Refer to the following example:

var async = require('async');

async.parallel([
    function(callback){
        while(true);//blocking!
    },
    function(callback){
        console.log('Not Blocked');//Never get's called
    }
]);

The second function will never get called. The parallel that async provides helps out, for exactly the reason you are confused about your example. The problem with asynchronous code is that, sometimes we have some series of callbacks for things that are actually parallel in node(disk I/O, network I/O, etc...) that need to complete, but that will end up completing at unpredictable intervals. Say for example we have configuration data to collect from multiple sources, and no sync methods are supplied. We don't want to run these serially, because this slows things down considerably, but we still want to collect the results in order. This is the prime example use for async.parallel. But, no, async.parallel cannot make synchronous code execute asynchronously, as is shown by this blocking example.

True parallelism in node comes within the V8 backend. The only way to provide this parallelism would be to release an alternate version of node or by developing native extensions.

Sign up to request clarification or add additional context in comments.

2 Comments

please tell me the different between async.series and async.parallel. I try it out by an example, but i didn't see the difference.
Async.series takes a series of asynchronous operations, and runs them in series, as if each function in the array were a callback to the function before it. Async.parallel takes a series of async operations, lets them run in parallel, and returns to program flow by calling a callback, ideally one that takes advantage of the data in the array that has been populated by parallel(otherwise why are you using it, right?)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.