Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
583 views
in Technique[技术] by (71.8m points)

node.js - difference between async.series and async.parallel

What is the difference between async.series and async.parallel. Consider the following exmaple, and i've got the same result.

async.parallel([
    function(callback){
        setTimeout(function(){
            callback(null, 'one');
        }, 200);
    },
    function(callback){
        setTimeout(function(){
            callback(null, 'two');
        }, 100);
    },
    function(callback){
        setTimeout(function(){
            var err = new Error('I am the error');
            callback(err);
        }, 400);
    },
    function(callback){
        setTimeout(function(){
            callback(null, 'three');
        }, 600);
    },
],
// optional callback
function(err, results){
    if(err){
        console.log('Error');
    } else {

    }
    console.log(results);
    //results is now equal to [ 'one', 'two', undefined ]
    // the second function had a shorter timeout.
});

and

async.series([
   function(callback){
        setTimeout(function(){
            callback(null, 'one');
        }, 200);
    },
    function(callback){
        setTimeout(function(){
            callback(null, 'two');
        }, 100);
    },
    function(callback){
        setTimeout(function(){
            var err = new Error('I am the error');
            callback(err);
        }, 400);
    },
    function(callback){
        setTimeout(function(){
            callback(null, 'three');
        }, 600);
    }
],
// optional callback
function(err, results){
    //results is now equal to [ 'one', 'two', undefined ]
    if(err){
        console.log('Error');
    } else {

    }
    console.log(results);
});

i don't see the difference. Maybe is my sample bad? I read the documentation about these two function on github async repository, you can find for async.parallel function:

If any of the functions pass an error to its callback, the main callback is immediately called with the value of the error

what is the main callback in async.parallel?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

async.series invokes your functions serially (waiting for each preceding one to finish before starting next). async.parallel will launch them all simultaneously (or whatever passes for simultaneous in one-thread land, anyway).

The main callback is the one optionally supplied in the call to async.parallel or async.series (The signature is async.parallel(tasks, [callback]))

So what actually happens is this:

parallel:

  • parallel launches all the tasks, then waits
  • all four tasks schedule their timeouts
  • timeout 100 fires, adds its result (result is now [ , "Two"])
  • timeout 200 fires, adds its result (result is now ["One", "Two"])
  • timeout 400 fires, returns error and undefined as result (result is now ["One", "Two", undefined])
  • parallel notices an error, immediately returns the result it received so far
  • timeout 600 fires, but no-one cares about the return result

series:

  • series fires the first task; it schedules its timeout.
  • series waits till callback is called 200ms later, then adds the result. (result is now ["One"])
  • series fires the second task; it schedules its timeout.
  • series waits till callback is called 100ms later, then adds the result. (result is now ["One", "Two"])
  • series fires the third task; it schedules its timeout.
  • series waits till callback is called 400ms later, then adds the result and exits due to error. (result is now ["One", "Two", undefined])
  • the fourth task is never executed, and its timeout is never scheduled.

The fact that you got the same result is due to the fact that you rely on setTimeout in your tasks.

As to how to use parallel usefully, try to download a hundred web pages with parallel; then do the same with series. See what happens.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...