async.series 和 async.parallel 的区别 [英] difference between async.series and async.parallel
问题描述
async.series 和 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.
});
和
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);
});
我看不出有什么区别.也许我的样品不好?我在github异步存储库上阅读了有关这两个函数的文档,您可以找到async.parallel函数:
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
async.parallel 中的主要回调是什么?
what is the main callback in async.parallel?
推荐答案
async.series
串行调用您的函数(等待前面的每个函数完成,然后再开始下一个).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).
主回调是在对 async.parallel
或 async.series
的调用中可选提供的回调(签名是 async.parallel(tasks, [回调])
)
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
启动所有任务,然后等待- 所有四个任务都安排超时
- 超时 100 次触发,添加其结果(结果现在是
[ , "Two"]
) - 超时 200 次触发,添加其结果(结果现在是
["One", "Two"]
) - 超时 400 触发,返回错误和
undefined
结果(结果现在是["One", "Two", undefined]
) parallel
发现错误,立即返回目前收到的结果- 超时 600 次触发,但没人关心返回结果
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
等待 200 毫秒后调用回调,然后添加结果.(结果现在是["One"]
)series
触发第二个任务;它安排超时.series
等待 100 毫秒后调用回调,然后添加结果.(结果现在是["One", "Two"]
)series
触发第三个任务;它安排超时.series
等待 400 毫秒后调用回调,然后添加结果并因错误退出.(结果现在是["One", "Two", undefined]
)- 第四个任务永远不会执行,它的超时时间也永远不会被安排.
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.
您得到相同结果的事实是由于您在任务中依赖 setTimeout
.
The fact that you got the same result is due to the fact that you rely on setTimeout
in your tasks.
关于如何有效地使用parallel
,尝试用parallel
下载一百个网页;然后对 series
做同样的事情.看看会发生什么.
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.
这篇关于async.series 和 async.parallel 的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!