Reputation: 2623
I have an array of directories.
var directories = ['/dir1', '/dir2'];
I want to read all files under these directories, and in the end have an object that matches filenames with their base64/utf8 data. In my case these files are images. The resulting object might look like:
var result = {
'/dir1': {
'file1': 'iVBORw0KGgoAAAANSUhEUgAAAOEAAADhCAIAAACx0UUtAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWF...',
'file2': 'iVBORw0KGgoAAAANSUhEUgAAAOEAAADhCAIAAACx0UUtAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWF...'
}
}
I easily implemented this with a callback hell, but when I try with Promises, I'm not sure how to pass directory information, and filename to succeeding then() and map() functions.
In the example below I'm using Bluebird library:
const Promise = require('bluebird'),
fs = Promise.promisifyAll(require('fs'));
var getFiles = function (dir) {
return fs.readdirAsync(dir);
};
Promise.map(directories, function(directory) {
return getFiles(directory)
}).map(function (files) {
// which directory's files are these?
return files;
})
The next step would be iterating over files and reading their data.
I don't mind if answer is with ES6, Bluebird, or Q.
Upvotes: 3
Views: 2289
Reputation: 665574
The easiest way with Bluebird would be to use props
:
function getFiles(paths) { // an array of file/directory paths
return Promise.props(paths.reduce(function (obj, path) {
obj[path.split("/").pop()] = fs.statAsync(path).then(function (stat) {
if (stat.isDirectory())
return fs.readdirAsync(path).map(function (p) {
return path + "/" + p;
}).then(getFiles);
else if (stat.isFile())
return fs.readFileAsync(path);
}).error(function (e) {
console.error("unable to read " + path + ", because: ", e.message);
// return undefined; implied
});
return obj;
}, {}));
}
Upvotes: 2
Reputation: 14926
I have no idea about Bluebird, but this is the ES6 Promise approach.
var fs = require('fs');
var directories = ['/dir1', '/dir2'];
var result = {};
Promise.all(directories.map(function(dir) {
result[dir] = {};
return new Promise(function(resolve, reject) {
fs.readdir(dir, function(er, files) {
if (er) {
return reject(er);
}
resolve(files);
});
}).then(function(files) {
return Promise.all(files.map(function(file) {
return new Promise(function(resolve, reject) {
fs.readFile(dir + '/' + file, function(er, data) {
if (er) {
return reject(er);
}
result[dir][file] = data.toString('base64');
resolve();
});
});
}));
});
})).then(function() {
console.log(result);
}).catch(function(er) {
console.log(er);
});
Upvotes: 1