为什么node.jsasynchronous模块在使用async.eachLimit(array,limit,function,callback)的第一步之后停止?

如果我使用这个代码:

async.eachLimit(body.photos.photo, 10, function(photo) { var flickr_getphoto_path = "....."; request.get({url: flickr_host_url + flickr_getphoto_path, json: true}, function(error, response, body) { if (!error && response.statusCode == 200) { console.log("SIZES LENGTH: " + body.sizes.size.length); var source_url = body.sizes.size[body.sizes.size.length - 1].source; request(source_url).pipe(fs.createWriteStream(path_for_downloads + path.basename(source_url))); } }); } 

处理在10个请求后(即在第一个周期之后)停止。 应该有10个周期。

有人知道为什么它不能正常工作?

你设置的asynchronousfunction是错误的。 第三个参数(迭代器函数)需要两个参数:被迭代的项目和一个callback,告诉asynchronous它已经完成。 你错过了(因此从来不会调用)callback,所以asynchronous不知道是时候做下一批。

 var async = require('async'); async.eachLimit(body.photos.photo, 10, cacheOnePhoto, function(err){ if(err){ console.log(err); } else { console.log('Processing complete'); }; }) function cacheOnePhoto(photo, done){ var flickr_getphoto_path = "....."; request.get({ url: flickr_host_url + flickr_getphoto_path, json: true }, function(error, response, body) { if (!error && response.statusCode == 200) { console.log("SIZES LENGTH: " + body.sizes.size.length); var source_url = body.sizes.size[body.sizes.size.length - 1].source; request(source_url).pipe( fs.createWriteStream(path_for_downloads + path.basename(source_url)) ); done(null); } else { done('Request error for '+flickr_getphoto_path); } }); };