i’m studying node.js stream module
and there’s one problem i can’t solve by myself.
const fs = require('fs')
const {Readable} = require('node:stream')
const rs = new Readable({
read(size) {
if(this.count < 3){
console.log('read: ', this.count)
this.push(`data${this.count + 1}t`)
this.count++
}else{
console.log('read: null')
this.push(null)
}
}
});
const ws = fs.createWriteStream('./big.txt')
rs.count = 0
rs.on('data', (c) => console.log('[data event]'))
rs.pipe(ws);
and big.txt is like this as i expected
data1 data2 data3
but the console.log is like this and this is not what i expected
read: 0
read: 1
[data event]
read: 2
[data event]
read: null
[data event]
i expected this
read: 0
[data event]
read: 1
[data event]
read: 2
[data event]
read: null
why is it different?
can anybody let me know? thanks