Skip to content Skip to sidebar Skip to footer

Is There A Way To Actually Pause Readline?

Consider the below code, input file and the results. The setTimeout is here to mimic an external call, such as http get. What is the best / simple way to keep the results in order?

Solution 1:

This is a limitation of how the readline module works in NodeJS. It pauses reading the input but does not do so immediately. This is in part because of what Node streams were designed to do.

pause and resume were meant to handle backpressure - which means their goal was to deal with producers which were "too fast" for their consumers to keep up with and in order to balance node. They were never designed in the context of guaranteeing things pausing immediately.

You can use lower level fs operations instead and reading the lines one by one or storing them in a buffer to overcome this. If the file is small enough - just doing fs.readFile on it and splitting by \n should probably be enough to then iterate it with promises and perform async actions.

Post a Comment for "Is There A Way To Actually Pause Readline?"