मैं एक सरणी पर पुनरावृत्त कर रहा हूं जिसमें फ़ाइल नाम हैं। उनमें से प्रत्येक के लिए, मैं 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