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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…