async.series 不执行第二个函数



我正在尝试使用异步。以顺序模式在不同的文件中执行2个函数。用例是将日志文件下载到本地,然后将它们移动到一个文件夹中。

函数单独工作很好。现在我的下一步是在index.js中需要这两个文件,并按顺序调用这两个函数。

这是将文件从webdav写入本地机器的代码:

const fs = require('fs');
const stream = require('stream');
const util = require('util');
const pipeline = util.promisify(stream.pipeline);
const { createClient } = require("webdav");
const path = require("path");
const client = createClient(
"https:/*/webdav/Sites/Logs", {
username: "*",
password: "*"
}
);
async function writeFiles(){
console.log('inside write functions');
let directoryData = await client.getDirectoryContents("/");
console.log('after directorydata' +directoryData);
await Promise.all(directoryData.map(start));

}
async function start(dir) {
let  fileName = dir.filename;
console.log('inside start function');
var directory = path.join(__dirname,'/logs/') 
if(fileName.includes('.log')){
try {
let fileName = dir.filename;
//console.log(fileName);
await pipeline(
client.createReadStream(fileName),
fs.createWriteStream(directory + fileName)
);
return directory;
console.log('done');
} catch (e) {
console.log(e);
}

} 
}
module.exports = writeFiles;

这是创建文件夹并根据名称将日志文件移动到其中的代码:

const testFolder = './logs/';
const fs = require('fs');
const path = require("path");
var directory = path.join(__dirname,'/logs/');
async function sort (){
console.log('inside sort function sorting file');
await fs.readdir(directory, (err, files) => {
//console.log('sdcfe');
if(files){
console.log('filesss')
} else {
console.log('no filesss')
}
files.forEach(file => {
let fileFolder = file.split('-')[0];
var folder = fileFolder.replace('.', "");
var dir = directory+folder;
if (!fs.existsSync(dir)){
//console.log('inside if' + dir);
fs.mkdirSync(dir);
} 
const currentPath = path.join(directory, file);
const destinationPath = path.join(directory, folder, file);
fs.rename(currentPath, destinationPath, function (err) {
if (err) {
throw err
} else {
return destinationPath;
console.log("Successfully moved the file!");
}
});
});
});
}
module.exports = sort;

这是index.js文件,需要上述两个文件并按顺序调用它们(一旦写函数完成,我需要将日志文件移动到文件夹中)

'use strict';
// Initialize constants
const async = require('async');

const writeFiles = require('./webdavConnect2');
const sort = require('./sortingFiles');

// Initialize the program
let program = require('commander');
// Initialize the version
program.version('0.5.0');

program
.command('say')
.description('Batch command that can be used to zip, upload, import, and re-index for a given site import.')
.action(function () {
function webdavConnect() {
console.log('inside webdav function');
// write the files into local machine
writeFiles();
}
// move the files into folders
function sortingFiles() {
console.log('inside sort function');

sort();

}

async.series([
function(callback) {
webdavConnect(function(directory){
callback(null, directory);
})
},
function(callback) { sortingFiles(function(destinationPath){
callback(null,destinationPath);
})
}

], function(asyncErrorObj) {
// Was an error defined?  If so, then throw it
if (asyncErrorObj !== null) { throw asyncErrorObj; }


console.log('END');
});
});

// Parse the command-line arguments
program.parse();

问题是当前执行的是第一个函数,而不是第二个函数。

张贴朋友建议的解决方案:

我指出了一些关于async使用的问题。系列。尝试用以下代码替换index.js。注意现在传递给async的async函数。系列,并注意这些函数现在如何接受Async.js应该传递给它们的回调,这些回调在当前代码中缺失。我无法对此进行测试,因为项目是不可复制的(至少在没有主要准备的情况下)。如果有什么坏了,你可以分享另一条消息和/或直接标记我!希望能有所帮助。

"use strict";
const async = require("async");
const writeFiles = require("./webdavConnect2");
const sort = require("./sortingFiles");
let program = require("commander");
program.version("0.5.0");
program
.command("say")
.description(
"Batch command that can be used to zip, upload, import, and re-index for a given site import."
)
.action(function () {
async function webdavConnect(callback) {
callback(await writeFiles());
}
async function sortingFiles(callback) {
callback(await sort());
}
async.series(
[
function (callback) {
webdavConnect(function (directory) {
callback(null, directory);
});
},
function (callback) {
sortingFiles(function (destinationPath) {
callback(null, destinationPath);
});
}
],
function (asyncErrorObj, results) {
if (asyncErrorObj !== null) {
throw asyncErrorObj;
}
console.log(results);
}
);
});
program.parse();

似乎这两个函数都是异步的,它们应该在async函数中调用await。第一个函数没有完成执行,第二个函数开始执行,但是由于它没有找到文件,所以您无法看到第二个函数的输出。这是我要做的修复执行问题:

"use strict";
const writeFiles = require("./webdavConnect2");
const sort = require("./sortingFiles");
// Initialize the program
let program = require("commander");
// Initialize the version
program.version("0.5.0");
program
.command("say")
.description(
"Batch command that can be used to zip, upload, import, and re-index for a given site import."
)
.action(async function() {
await writeFiles();
await sort();
});
// Parse the command-line arguments
program.parse();

相关内容

  • 没有找到相关文章

最新更新