In a Node.js project, I am using a for await of
loop with a readable stream as data source, so my code conceptually looks like this:
for await (const item of readableStream) {
// ...
}
The readable stream exists before the loop, i.e. it does not get created for the loop, but it is already there. Now the point is that I would like to be able to stop listening to this stream, i.e. I would like to break
from the loop, but I do not want to close the readable stream.
If I simply do this:
for await (const item of readableStream) {
if (condition) {
break;
}
// ...
}
the loop stops as expected, but the stream gets closed. How can I achieve the same thing, without closing the stream?
I can't use on('data')
directly, because the action I want to take includes running an async
function, and hence this won't work. I know that I could solve this using a Transform
or a Writable
stream, but this seems to be some overhead I would like to avoid.
Is what I want to do possible with a for await of
loop, and if so, how? If not, what is my best alternative?
data
. Does this result in desired behaviour? – Rickey