In a node program I’m reading from a file stream with fs.createReadStream. But when I pause the stream the program exits. I thought the program would keep running since the file is still opened, just not being read.
Currently to get it to not exit I’m setting an interval that does nothing.
setInterval(function() {}, 10000000);
When I’m ready to let the program exit, I clear it. But is there a better way?
Example Code where node will exit:
var fs = require('fs');
var rs = fs.createReadStream('file.js');
rs.pause();
Node will exit when there is no more queued work. Calling
pauseon aReadableStreamsimply pauses thedataevent. At that point, there are no more events being emitted and no outstanding work requests, so Node will exit. ThesetIntervalworks since it counts as queued work.Generally this is not a problem since you will probably be doing something after you pause that stream. Once you
resumethe stream, there will be a bunch of queued I/O and your code will execute before Node exits.Let me give you an example. Here is a script that exits without printing anything:
The stream is paused, there is no outstanding work, and my callback never runs.
However, this script does actually print output:
In conclusion, as long as you are eventually calling
resumelater, you should be fine.