difference between async.series and async.parallel
node.js
Solution
`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.
Problem
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?