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?
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 launches all the tasks, then waits[ , "Two"])["One", "Two"])undefined as result (result is now ["One", "Two", undefined])parallel notices an error, immediately returns the result it received so farseries 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 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With