Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

修改了对eachLimit的错误理解 #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions each.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ async.eachSeries(arr,function(item, callback) {
// 42.460> 1.4 err: myerr

/**
* 分批执行,第二个参数是每一批的个数。每一批内并行执行,但批与批之间按顺序执行
* 分批执行,第二个参数是同步执行的上限个数。当有任务完成时,就拿入新任务补充
*/
async.eachLimit(arr, 2, function(item, callback) {
log('1.5 enter: ' + item.name);
Expand All @@ -118,28 +118,32 @@ async.eachLimit(arr, 2, function(item, callback) {
});
// 42.247> 1.5 enter: Jack
// 42.248> 1.5 enter: Mike
// 42.351> 1.5 handle: Mike
// 42.352> 1.5 enter: Freewind
// 42.351> 1.5 handle: Mike /* 一个任务完成
// 42.352> 1.5 enter: Freewind /* 立即加入一个新的任务
// 42.461> 1.5 handle: Jack
// 42.664> 1.5 handle: Freewind
// 42.664> 1.5 err: undefined

/**
* 如果中途出错,错误将马上传给最终的callback。同一批中的未执行完的任务还将继续执行,但下一批及以后的不再执行
* 如果中途出错,错误将马上传给最终的callback。当有任务完成时,依然拿入新任务补充
*/
async.eachLimit(arr,2,function(item, callback) {
log('1.6 enter: ' +item.name);
setTimeout(function() {
log('1.6 handle: ' + item.name);
if(item.name==='Jack') {
callback('myerr');
}
}else{
callback();
}
}, item.delay);
}, function(err) {
log('1.6 err: ' + err);
});
// 42.248> 1.6 enter: Jack
// 42.248> 1.6 enter: Mike
// 42.352> 1.6 handle: Mike
// 42.352> 1.6 enter: Freewind
// 42.462> 1.6 handle: Jack
// 42.462> 1.6 err: myerr
// 42.462> 1.6 err: myerr
// 42.463> 1.6 handle: Freewind