I have function, which in perfect world should create an huge 1M lines file. Here it is:
const fileWriteStream = fs.createWriteStream(path.resolve(filePath));
let ableToWrite = true;
for (let i = 0; i < 1e6; i++) {
if (ableToWrite) {
ableToWrite = fileWriteStream.write(`.testClass${itr}-${i%2} { background: red } \n`);
} else {
fileWriteStream.once('drain', () => {
ableToWrite = fileWriteStream.write(`.testClass${itr}-${i%2} { background: red } \n`);
})
}
}
Unfortunetly for me, i'm getting the following error pretty fast:
MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 drain listeners added. Use emitter.setMaxListeners() to increase limit
I don't really want to increase listeners count for this function. What is correct way to write such a big file using streams?
Many thanks!
ableToWritestill being false) it continues to add all the listeners - everything before even the first event fires. And when it fires, it fires all the listeners waiting for it.