为什么node.js异步模块在第一步使用async.eachLimit(数组、限制、函数、回调)后停止?
如果我使用此代码:为什么node.js异步模块在第一步使用async.eachLimit(数组、限制、函数、回调)后停止?,node.js,asynchronous,concurrency,Node.js,Asynchronous,Concurrency,如果我使用此代码: 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
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个周期
有人知道它为什么不能正常工作吗?您设置的异步函数错误。第三个参数(迭代器函数)接受两个参数:被迭代的项,以及一个回调来告诉async它已经完成了。您缺少(因此从未调用)回调,因此async不知道是时候进行下一批处理了
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);
}
});
};
注意,我不确定管道文件是如何工作的。您可能需要了解如何在管道完成时设置回调,然后才调用done()。