I need to access a directory and I will use stream with generator, and for each file I will push to myArray array. How do I return the filled array, in which case it is returning empty, even after iterating through the stream?
const { Readable, Transform, Writable } = require('node:stream')
const myArray = []
class ReadStream extends Readable {
i = 0
_read() {
if (this.i >= 10) {
this.push(null)
} else {
this.i++
const str = String(this.i)
const buf = Buffer.from(str, 'ascii')
console.clear()
this.push(buf)
}
}
}
class TransformStream extends Transform {
_transform(chunk, encoding, callback) {
const transformed = chunk.toString().toLowerCase()
callback(null, Buffer.from(String(transformed)))
}
}
class WriteStream extends Writable {
_write(chunk, encoding, callback) {
const write = chunk.toString()
myArray.push(write)
callback()
}
}
new ReadStream().pipe(new TransformStream()).pipe(new WriteStream())
How would I return the filled myArray array in this case?