I’m trying to read a lot of files and combine the information from them into one. Here is the implementation that I got. I want to rewrite it to .pipe(), but it only turns out to overwrite the file at each iteration. I’m asking for help.
async joinSlices(path) {
const wrs = new WriteStream('./bigJSON.json');
const files = fs.readdirSync(path);
for (let file of files) {
console.log(file + ' start');
await new Promise((res, rej) => {
const rs = new ReadStream(path + file, 'utf-8');
rs.on('data', (chunk) => { wrs.write(chunk); });
rs.on('end', (data) => { console.log(file + ' end'); });
rs.on('close', (data) => { res(); });
});
}
wrs.destroy();
console.log('write stream end');
}
The cycle starts if both streams are declared in its body and a promise is placed at the end of recording, but overwriting occurs at each iteration.
New contributor
user25010368 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.