When I use the filemanagerfunction for directory ( /), the code works fine, but when I call file ( /index.html), the code returns an error.
I see that the problem is in the if / else statement (it readdiris executed even if isDirreturned false), but I do not know how to use it correctly with promises.
var fs = require('fs'),
Q = require('q'),
readdir = Q.denodeify(fs.readdir),
readFile = Q.denodeify(fs.readFile);
function isDir(path) {
return Q.nfcall(fs.stat, __dirname + path)
.then(function (stats) {
if (stats.isDirectory()) {
return true;
} else {
return false;
}
});
}
function filemanager(path) {
if (isDir(path)) {
return readdir(__dirname + path)
.then(function (files) {
return files.map(function (file) {
return ...;
});
})
.then(Q.all);
} else {
return readFile(__dirname + path, 'utf-8')
.then(function (content) {
return ...;
});
}
}
filemanager('/').done(
function (data) {
...
},
function (err) {
...
}
);
source
share