파일 이름을 포함하는 배열을 반복합니다. 각각에 대해 readFile()
을 호출합니다. 해당 콜백이 호출되면 readFile()
에 전달 된 파일 이름을 매개 변수로 검색하려고합니다. 할 수 있습니까?NodeJS readFile() 파일 이름 검색
본인의 의도를 더 잘 설명 할 수 있도록 코드를 동봉했습니다.
var fs = require("fs");
var files = ["first.txt", "second.txt"];
for (var index in files) {
fs.readFile(files[index], function(err, data) {
//var filename = files[index];
// If I am not mistaken, readFile() is asynchronous. Hence, when its
// callback is invoked, files[index] may correspond to a different file.
// (the index had a progression).
});
}
은 아래의 모든 솔루션을 확인하십시오. 각각은 정확합니다. – MrIzik