Skip to content
Tutorials Center
My learning

Topics

Node.js Reference ยท Streams and readline

Node.js readable.pause() and readable.resume()

Reference for readable.pause(), readable.resume() and isPaused() in Node.js: switch between flowing and paused mode and throttle a stream by hand.

A readable stream is in one of two modes. In flowing mode it pushes chunks to 'data' listeners as fast as it can. In paused mode nothing is delivered until you call read(). readable.pause() stops the flow, and readable.resume() starts it again.

Use them to throttle a producer while the consumer is busy, for example while an asynchronous write is in flight. Calling resume() on a stream with no 'data' listener is also the standard way to throw its data away and let it reach 'end'.

NODE
readable.pause();   // stop emitting 'data'
readable.resume();  // continue
readable.isPaused();

Try it

Node.js Pausing between chunks

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Node.js Draining with resume()

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Syntax

NODE
readable.pause(): this
readable.resume(): this
readable.isPaused(): boolean
Name Description
pause() Switches a flowing stream to paused mode. Returns the stream.
resume() Makes the stream flow, emitting 'data' events (or discarding data when there is no listener). Returns the stream.
isPaused() true if the stream was explicitly paused. A fresh stream that has not started flowing reports false.

Edge cases: readable.readableFlowing has three states: null (no consumer yet), true (flowing) and false (explicitly paused, or unpiped with unpipe()).

Usage notes

  • Adding a 'data' listener starts flowing mode automatically, unless the stream was explicitly paused before. Adding 'readable' instead puts the stream in paused mode.
  • Removing 'data' listeners does not pause a stream. Chunks are then lost. Call pause() first if you want to keep them.
  • If a 'readable' listener is attached, resume() has no effect: the 'readable' mechanism takes precedence until the listener is removed.
  • Piping handles pausing for you: pipe() pauses the source when the destination's buffer is full and resumes on 'drain'. Manual pause() is mostly useful with plain 'data' handlers.
  • pause() does not stop the underlying resource. The internal buffer keeps filling up to highWaterMark, then the stream stops asking its source for more.
  • Prefer async iteration when your processing is asynchronous: it applies backpressure automatically.