async.series

时间:2024-01-13 17:19:20

async.series

  async.series

  series适用于顺序执行异步且前后无关联的调用。对于顺序执行异步且前后有叛逆的调用,则需要使用waterfall

  If any functions in the series pass an error to its callback, no more functions are run, andcallback is immediately called with the value of the error. Otherwise, callback receives an array of results when tasks have completed.

  

async.series([
function(callback) {
// do some stuff ...
callback(null, 'one');
},
function(callback) {
// do some more stuff ...
callback(null, 'two');
}
],
// optional callback
function(err, results) {
// results is now equal to ['one', 'two']
}); async.series({
one: function(callback) {
setTimeout(function() {
callback(null, 1);
}, 200);
},
two: function(callback){
setTimeout(function() {
callback(null, 2);
}, 100);
}
}, function(err, results) {
// results is now equal to: {one: 1, two: 2}
});

参考:https://caolan.github.io/async/docs.html#series

相关文章