Javascript中的Async/await逻辑



我有一个代码,可以帮助我重命名多个文件与一个数据。在数据(rarityList)中,有我拥有的每个文件的名称和值。为了这个帖子的缘故,我只是用一个虚拟值替换了这些值,但你可以想象有500个。

它工作了,但以一种我意想不到的方式完成了工作。在终端中,它首先打印0(total),然后复制所有文件,然后重命名它。但我期待看到的是"复制已经完成了!"重命名完成!最后是终端的总数量。你能给我解释一下我遗漏了什么吗?

const fs = require("fs");
const path = "C:\Users\shepard\Desktop\Head";
const newPath = "C:\Users\shepard\Desktop\NewHead";
let files = fs.readdirSync(path);
const rarityList = [{name: "a", value: 3},{name: "b", value: 2}];
let total = 0;
const copy = async (_e) => {
fs.copyFile(path + "\" + _e, newPath + "\" + _e, err => {if (err) {console.log(err)}});
};
const rename = async (_e, _index) => {
let t = _e.replace(".png", "#" + rarityList[_index].value + ".png")
fs.rename(newPath + "\" + _e, newPath + "\" + t, err => {if (err) {console.log(err)}});
};
files.forEach(async (e) => {
await copy(e);
console.log("Copying is done!");
rarityList.forEach(async (element, index) => {
if (element.name == e.slice(0,-4)) {
await rename(e,index);
console.log("Renaming is done! for " + e + " with the rarity weight of " + rarityList[index].value);
total += rarityList[index].value;
};
});
});
console.log(total);

可能不是最好的答案,但却是一个解决方案:

const fs = require("fs");
const path = "C:\Users\shepard\Desktop\Head";
const newPath = "C:\Users\shepard\Desktop\NewHead";
let files = fs.readdirSync(path);
const rarityList = [
{ name: "a", value: 3 },
{ name: "b", value: 2 },
];
let total = 0;
const copy = async (_e) => {
fs.copyFile(path + "\" + _e, newPath + "\" + _e, (err) => {
if (err) {
console.log(err);
}
});
};
const rename = async (_e, _index) => {
let t = _e.replace(".png", "#" + rarityList[_index].value + ".png");
fs.rename(newPath + "\" + _e, newPath + "\" + t, (err) => {
if (err) {
console.log(err);
}
});
};
const renameFiles = async () => {
for (let e of files) {
await copy(e);
let index = 0;
for (let element of rarityList) {
if (element.name == e.slice(0, -4)) {
await rename(e, index);
console.log(
"Renaming is done! for " +
e +
" with the rarity weight of " +
rarityList[index].value
);
total += rarityList[index].value;
}
index += 1;
}
}
console.log('in async func', total);
};
renameFiles();
console.log('out of async function', total);

技巧在于async函数没有等待,所以total不会在"顶层"更新。你可以做一个"顶层"像这样等待:

const fs = require("fs");
const path = "C:\Users\shepard\Desktop\Head";
const newPath = "C:\Users\shepard\Desktop\NewHead";
let files = fs.readdirSync(path);
const rarityList = [
{ name: "a", value: 3 },
{ name: "b", value: 2 },
];
let total = 0;
const copy = async (_e) => {
fs.copyFile(path + "\" + _e, newPath + "\" + _e, (err) => {
if (err) {
console.log(err);
}
});
};
const rename = async (_e, _index) => {
let t = _e.replace(".png", "#" + rarityList[_index].value + ".png");
fs.rename(newPath + "\" + _e, newPath + "\" + t, (err) => {
if (err) {
console.log(err);
}
});
};
const renameFiles = async () => {
for (let e of files) {
await copy(e);
let index = 0;
for (let element of rarityList) {
if (element.name == e.slice(0, -4)) {
await rename(e, index);
console.log(
"Renaming is done! for " +
e +
" with the rarity weight of " +
rarityList[index].value
);
total += rarityList[index].value;
}
index += 1;
}
}
console.log("in function", total);
};
(async function() {
await renameFiles()
console.log(total)
}());

相关内容

  • 没有找到相关文章

最新更新