我在向文件中写入内容时正在使用的函数中遇到一些问题。在此函数中,我想显示刚刚写入的文件名,但是,我的错误函数仅显示最后一个文件名,而不显示其余文件名。为什么是这样?是范围问题吗?
我的代码
var myArray = ["A","B","C"]
// File variable
const fileHandler = require('fs');
for (let currentArrayElement in myArray ){
hexFile = myArray[currentArrayElement];
hexFile += ".hex"
console.log("hexFile---->"+hexFile);
// Writting serilaNumberInHex into a .hex file called hexFile
fileHandler.writeFile(hexFile,"blabla" , function(err) {
if(err) {
return console.log(err);
}
console.log("[Info] File " + hexFile + " was succesfully created");
});
}
输出
hexFile---->A.hex
hexFile---->B.hex
hexFile---->C.hex
[Info] File C.hex was succesfully created
[Info] File C.hex was succesfully created
[Info] File C.hex was succesfully created
[Finished in 0.2s]
预期产量
hexFile---->A.hex
hexFile---->B.hex
hexFile---->C.hex
[Info] File A.hex was succesfully created
[Info] File B.hex was succesfully created
[Info] File C.hex was succesfully created
[Finished in 0.2s]
[解决方案]感谢杜尔加(Durga)
我的代码
var myArray = ["A","B","C"]
// File variable
const fileHandler = require('fs');
for (let currentArrayElement in myArray ){
let hexFile = myArray[currentArrayElement];
hexFile += ".hex"
console.log("hexFile---->"+hexFile);
// Writting serilaNumberInHex into a .hex file called hexFile
fileHandler.writeFile(hexFile,"blabla" , function(err) {
if(err) {
return console.log(err);
}
console.log("[Info] File " + hexFile + " was succesfully created");
});
}