I am trying to read the contents of several files in Node.js using promises. Since the standard fs
module does not provide a sufficient promise interface, I decided to use fs-extra
instead which provides pretty much the same functions as the default fs
module with an additional promise interface.
Reading the contents of a single file as shown below works as desired and logs the file's contents to the console:
const fse = require('fs-extra')
const filePath = './foo.txt'
fse.readFile(filePath, 'utf8')
.then(filecontents => {
return filecontents
})
.then(filecontents => {
console.log(filecontents)
})
However, I need to handle several files inside a given directory. To do this I need to implement the following steps:
- get an array of all files inside the directory using
fse.readdir()
- done - join filenames and directory name to get a kind of a base file path using
path.join
with.map()
to avoid iterating over the array - done - read file contents using
fse.readFile()
inside another.map()
These three steps are implemented as follows:
const fse = require('fs-extra');
const path = require('path');
const mailDirectory = './mails'
fse.readdir(mailDirectory)
.then(filenames => {
return filenames.map(filename => path.join(mailDirectory, filename))
})
.then(filepaths => {
// console.log(filepaths)
return filepaths
.map(filepath => fse.readFile(filepath).then(filecontents => {
return filecontents
}))
})
.then(mailcontents => {
console.log(mailcontents)
})
As stated above, steps 1 and 2 are working quite nice. However, I am unable to read the file contents using fse.readFile()
inside the last .map()
which results in an
[ Promise { <pending> },
Promise { <pending> },
Promise { <pending> },
Promise { <pending> },
Promise { <pending> } ]
output indicating that the promise is not resolved, yet. I assume that this unresolved promise is the promise returned by the fse.readFile()
function. However I am unable to resolve it properly since a comparable approach in my very first snippet works like a charm.
How could I solve this issue? Where does it exactly come from since I am a newbie in the field of JS and especially in the field of Node.js?
Promise.all()
? – zero298 Oct 2 '17 at 19:07