这是我的代码片段:
var processListing = function (directoryItems) {
console.log('foreach');
var itemsToDownload = [];
directoryItems.forEach(function (element, index, array) {
//Ignore directories
if (element.type === 'd') {
console.log('directory ' + element.name);
return;
}
//Ignore non zips
if (path.extname(element.name) !== '.zip') {
console.log('ignoring ' + element.name);
return;
}
//Download zip
itemsToDownload.push({
source: element.name,
destination: element.name
});
//aftpSystem.downloadFile(element.name, element.name);
});
console.log('after foreach');
return itemsToDownload;
};
var getFiles = function () {
console.log('got files');
return fs.readdirAsync(process.cwd() + "/zips/").then(function (files) {
return files.filter(function (filename) {
return path.extname(filename) === '.zip';
});
});
};
aFtpClient. //this has been promisified
listAsync(). //so calls methodAsync
then(processListing).
map(function (object) {
return processItem(object).then(function (processResult) {
return {
input: object,
result: processResult
};
});
}).
map(function (downloadItem) {
console.log('downloading files');
downloadItem.result.once('close', function () {
console.log('closed');
});
return downloadItem.result.pipe(fs.createWriteStream(process.cwd() + "/zips/" + downloadItem.input.destination));
}).
then(getFiles).
我正在尝试使用承诺通过 FTP 下载项目。目前它下载第一个文件,但随后的文件失败。我是 node 新手,但相当有信心我的第二个 map 函数需要返回一个承诺,但是经过多次尝试后我无法弄清楚如何。我正在使用bluebird
承诺,但看不到如何使用它和流。
你能指出我正确的方向吗?
谢谢