To utilize Array.map
, follow this approach:
var files = ["./file1.txt", "./file2.txt"]
Promise.all(files.map(async file=>{ /* read file content */}))
If you prefer not to use an async function but still want it to return a promise, that works as well
var files = ["./file1.txt", "./file2.txt"]
Promise.all(files.map(file=>{ /* read file content, return a promise */}))
[edit] here's a demonstration using my favored file interface, fse - essentially fs with promises integrated:
var files = ["./file1.txt", "./file2.txt"]
Promise.all(files.map(file=>{
return fse.readFile(file, 'utf-8');
})).then(results => {
// results is an array of strings of the contents of each file
})
https://www.npmjs.com/package/fs-extra