我有async.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);
});
}
答案 0 :(得分:-1)
尝试将回调函数更改为cb
function checkAndDownloadPlaylist(data,cb) {
async.eachSeries(data,function(val,cb) {
url=val.media[0];
fileSize=val.fileSize;
checkAndDownloadItem(url,fileSize, cb);
}, function(error) {
if(error) {
throw error;
}
console.log('Done!');
});
}