I want to call a function that will iterate over files in a directory recursively, and return a resolve/reject on end. How can I do this?
readRecursive(location, (filename, content) => {
writeChanged(filename, content);
})
.then(() => {
console.log('Done!');
});
This is what I want to do - for each file, change something and rewrite it. And do something afterwards.
var readRecursive = (destination, callback) => {
fs.readdir(destination, (err, filenames) => {
if (err) {
console.error(err);
return false;
}
filenames.forEach(filename => {
fs.readFile(destination + filename, 'utf-8', (err, content) => {
if (err) {
console.error(err);
return false;
}
callback(destination + filename, content);
});
});
});
};
The problem is that if I return the promise here, it will call then after the first file is read, and skip the rest. What's the best way to solve this?
via user99999
No comments:
Post a Comment