0
我有一个与async.eachSeries问题 - 我用它来迭代数组和使用函数下载文件(请参阅源代码) - 但回调从未被称为...我在做什么有问题?我不知道的回调...异步eachSeries - 回调从未调用
function checkAndDownloadPlaylist(data,cb) {
async.eachSeries(data,function(val,callback) {
url=val.media[0];
fileSize=val.fileSize;
checkAndDownloadItem(url,fileSize, callback);
}, function(error) {
// never called...
if(error) {
return cb(error);
}
cb();
});
}
function checkAndDownloadItem(url,fileSize,cb) {
if(checkedUrls.indexOf(url)==-1) {
fileName=getFileNameFromUrl(url);
filePath=path.join(config.movieDir,fileName);
// save file in usedFiles[]
d=new Date();
usedFiles[fileName]=d.toISOString();
fs.access(filePath, fs.constants.F_OK, function(error) {
checkedUrls.push(url);
if(error) {
// file is not there
winston.debug('file Missing, start DL: '+fileName);
download(url,filePath,cb);
}else{
// file is there
winston.debug('file already there: '+fileName);
cb(null);
}
});
}
}
function download(url,destination,cb) {
winston.debug('start DL: '+url);
var tmpDestination=destination+'.tmp';
file=fs.createWriteStream(tmpDestination);
request = http.get(url, function(response) {
response.pipe(file);
file.on('finish', function() {
winston.debug('finished DL: '+url);
file.close();
fs.renameSync(tmpDestination,destination);
cb(null);
});
}).on('error', function(error) {
fs.unlinkSync(tmpDestination);
if(error) {
cb(error.message);
winston.log('notice','error DL: '+url);
}
cb(null);
});
}
是的,有相当多的情况下, 'cb'永远不会被调用;修复它们。 – Bergi
该死的,现在我明白了 - 谢谢:) – cklm