I am trying to read and parse a local folder consisting of CSV files asynchronously. I have managed to figure out how to do it by hardcoding the files paths
const fsPromises = require('fs').promises
const parse = require('csv-parse/lib/sync')
const fs = require('fs')
async function readfile() {
try {
const fileData = await Promise.all([
fsPromises.readFile(__dirname + '/testdata/a1.csv').then((file) =>
parse(file, {
columns: ['Date', 'Name', 'Value'],
relax_column_count: true,
fromLine: 2,
})
),
fsPromises.readFile(__dirname + '/testdata/b1.csv').then((file) =>
parse(file, {
columns: ['Date', 'Name', 'Value'],
relax_column_count: true,
fromLine: 2,
})
),
])
fileDataFlattened = fileData.flat()
console.log(fileDataFlattened)
} catch (err) {
console.log(err)
}
}
readfile()
I would now like to do it without having to hardcode the files. I figured out you could do this by creating an array of promises which with the functionality to read and parse the files from an array of file paths and using Promise.all to resolve the promises.
Unfortunately, I can't seem to figure out how to create this array of promises. Please help.
const fsPromises = require('fs').promises
const parse = require('csv-parse/lib/sync')
const fs = require('fs')
// reading an array of filenames from the path
const filePaths = fs.readdirSync(__dirname + '/testdata')
// creating an array of promises to read and parse the file as per the above code
[array of promises] = .......
async function readDirectory() {
.....
const fileData = await Promise.all([array of promises])
.....
}
readDirectory()