I've dealing too many hours for such task in for node.js. I'm mainly front-end guy.
I find this quite important, because all node methods asyncronous deal with callback, and transform it into Promise is better to handle it.
I Just want to show a possible outcome, more lean and readable. Using ECMA-6 with async you can write it like this.
async function getNameFiles (dirname) {
return new Promise((resolve, reject) => {
fs.readdir(dirname, (err, filenames) => {
err !== (undefined || null) ? reject(err) : resolve(filenames)
})
})
}
the (undefined || null)
is for repl (read event print loop) scenarios,
using undefined also work.