场景
需要并行运行两个任务,并在firstTask && secondaryTask
完成后运行第三个任务。一直在使用async
库并且代码可以工作,但想知道我的代码是否有一些可以改进的地方。
详细信息
任务 1:readFileNames
:读取文件夹并返回文件名数组。
任务 2:copyFile
:将 config
文件从 src 文件夹复制到目标文件夹。
任务 3:writeConfig
:将 readFileNames
的结果写入位于 destination
文件夹的 config
文件中.
问题
我应该将并行
控制流与eachSync
结合起来吗?另外,想知道 promise 是否可以帮助我实现我想要做的事情?哪种方法在性能方面更好? Async vs Q或者我应该使用更抽象的库,例如orchestrator
?
下面是我到目前为止所做的,它有效,但想知道是否有更好的方法:
代码
var async = require("async");
var fs = require("fs-extra");
var dir = require("node-dir");
var path = require("path");
var _ = require("underscore");
var readFileNames = function (src, cb) {
dir.files(src, function (err, files) {
if (err) { return cb(err); }
return cb(files);
});
};
var copyFile = function (src, dest, cb) {
fs.copy(src, dest, function (err) {
if (err) { return cb(err); }
return cb();
});
};
var writeConfig = function (destFile, content, cb) {
fs.appendFile(destFile, content, function (err) {
if (err) { return cb(err); }
return cb();
});
};
var modulesFolder = path.join(__dirname, "modules");
var srcFile = path.join(__dirname, "src", "config.json");
var destFile = path.join(__dirname, "dest", "config.json");
async.parallel(
[
function (callback) {
readFileNames(modulesFolder, function (files) {
callback(null, files);
});
},
function (callback) {
copyFile(srcFile, destFile, function () {
callback(null, "");
});
}
],
// last callback
function (err, results) {
var toWrite = _.flatten(results);
toWrite.forEach(function (content) {
if(content) {
writeConfig(destFile, content + "\n", function () {
});
}
});
console.log("done");
}
);
文件
├── dest
├── main.js
├── modules
│ ├── module1.txt
│ └── module2.txt
├── node_modules
│ ├── async
│ ├── fs-extra
│ └── node-dir
├── package.json
└── src
└── config.json
最佳答案
I have been using async library and the code works, but want to know if there is something I could improve about my code.
您使用了太多的匿名函数表达式。只需传递您收到的回调
即可!所有这些辅助函数(包括命名的函数)都是相当多余的。
此外,您的 readFileNames
函数不遵循 Node 回调约定。我假设您不打算将错误写入结果文件中?
此外,您的最终回调确实会忽略错误。
Should I combine the parallel control flow with eachSync?
我猜你的意思是eachSeries
,不是 eachSync
?是的,如果您希望 appendFile
调用无论如何都会链接起来,那么这是合理的。但是您也可以再次使用parallel
,这将与您当前正在执行的.forEach
更紧密地相关。但以防万一,您应该使用async
迭代方法,因为目前您记录“done”
太早了。
所以宁愿这样做
var async = require("async");
var fs = require("fs-extra");
var dir = require("node-dir");
var path = require("path");
var modulesFolder = path.join(__dirname, "modules");
var srcFile = path.join(__dirname, "src", "config.json");
var destFile = path.join(__dirname, "dest", "config.json");
async.parallel([
function (callback) {
dir.files(modulesFolder, callback);
},
function (callback) {
fs.copy(srcFile, destFile, callback);
}
], function(err, results) {
if (err)
return console.error(err);
var toWrite = results[0] // ignore the result of the copy action
async.eachSeries(toWrite, function(content, callback) {
// if(content) not sure if needed at all
fs.appendFile(destFile, content + "\n", callback);
}, function(err) {
if (err)
return console.error(err);
console.log("done");
});
});
Also, was wondering if promises would help me achieve what I am trying to do?
是的,他们也可以做到这一点,而且可能更容易、更直接(如果你习惯的话)。它们还会大大简化错误处理。
And which approach is better in terms of performance?
都不是。这个小脚本的性能取决于系统的 IO 功能以及算法使用的并行化程度。这可以通过任一库来实现。
关于javascript - Node.js:使用异步库并行运行两个任务,并在前两个任务完成后运行最后一个任务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30108302/