【发布时间】:2019-10-28 01:56:49
【问题描述】:
我构建了一个 Angular/Node 应用程序,用于重命名网络文件夹中的文件。它重命名的文件数量在 300 到 500 之间。我使用 await 以便在重命名完成时收到通知。每次运行需要 8-10 分钟,因为我使用的是 await,所以不能同时重命名。
我需要传递重命名文件的数量,并且我需要向用户显示重命名已经完成。如果我不使用 async/await,我的 Angular 前端怎么知道重命名完成了?
我的完整代码在这里:https://github.com/ericute/renamer
这就是我遇到问题的地方:
await walk(folderPath, function(err, results) {
if (err) throw err;
results.forEach(file => {
if (fs.lstatSync(file).isFile) {
fileCounter++;
}
let fileBasename = path.basename(file);
let filePath = path.dirname(file);
if (!filesForRenaming[path.basename(file)]) {
//In a javascript forEach loop,
//return is the equivalent of continue
//https://stackoverflow.com/questions/31399411/go-to-next-iteration-in-javascript-foreach-loop
return;
}
let description = filesForRenaming[path.basename(file)].description;
// Process instances where the absolute file name exceeds 255 characters.
let tempNewName = path.resolve(filePath, description + "_" + fileBasename);
let tempNewNameLength = tempNewName.length;
let newName = '';
if (tempNewNameLength > 255) {
let excess = 254 - tempNewNameLength;
if (description.length > Math.abs(excess)) {
description = description.substring(0, (description.length - Math.abs(excess)));
}
newName = path.resolve(filePath, description + "_" + fileBasename);
} else {
newName = tempNewName;
}
renamedFiles++;
// Actual File Renaming
fs.renameSync(file, newName, (err) => {
if (err) {
errList.push(err);
}
renamedFiles++;
});
});
if (Object.keys(errList).length > 0) {
res.send({"status":"error", "errors": errList});
} else {
res.send({
"status":"success",
"filesFoundInDocData": Object.keys(filesForRenaming).length,
"filesFound": fileCounter,
"renamedFiles": renamedFiles,
"startDate": startDate
});
}
});
【问题讨论】:
-
您的代码应该会抛出错误,因为您不会等待这样的函数定义。此外,没有理由使用它,因为您使用的是 fs 命令的 *Sync 版本,它们不是异步的......
-
嗨@HereticMonkey,感谢您检查我的问题。如果我不使用同步,如何收到重命名完成的通知?这就是我需要的:(1)进行重命名(2)在不阻塞进程的情况下得到通知。
-
非同步版本允许您传递回调。或者你可以使用
util.promisfy来做出承诺,这样你就可以使用 async/await。有许多关于在 Node 中异步执行操作的教程和问题。