Reputation: 89
I wrote a function that returns a resolved promise after piping a readable stream. There are two pipes. The first pipe is a transform, and the second pipe is a writable stream. I do not want the writable stream to be closed, so I pass {end:false}
in the writer/pipe argument. The transform and writable stream work independently (ie one pipe only) but not when I chain them.
As per all examples I've seen, in order to know when the process is complete I added readable.on('end')
. It is not working as expected.
pipe(readable, transform, writable){
return new Promise((resolve, reject) => {
readable
.pipe(transform)
.pipe(writable, {end:false})
.on('error', (error) => {
console.error(error)
reject(error);
})
readable.on('end', () => {
writable.end();
resolve();
});
});
}
writable.end()
nothing is written to the filewritable.end()
it actually does exactly what it is supposed to do, BUT it resolves the promise way too early.. the file is still being written to.{end:false}
and just do this normally, it works fine. The problem is I want the writable stream to stay open.Upvotes: 3
Views: 1664
Reputation: 89
After trying every event, it is the writable.on('unpipe')
handler that I needed to indicate writing was done.
pipe(readable, transform, writable){
return new Promise((resolve, reject) => {
readable
.pipe(transform)
.pipe(writable, {end:false})
.on('unpipe', () => {
resolve();
// this must be called somewhere!
// writable.end();
})
.on('error', (error) => {
console.error(error)
reject(error);
})
});
}
Upvotes: 1