Why destroy a stream by mistake?

I see several modules that handle read streams in write streams, and if any error occurs, they use the destroy method:

const readable = fs.createReadStream("file");
const writable = fs.createWriteStream("file2");

readable.pipe(writable);

readable.on("error", (error) => {
  readable.destroy();
  writable.destroy();
  writable.removeListener("close");
  callback(error);
});

writable.on("error", (error) => {
  readable.destroy();
  writable.destroy();
  writable.removeListener("close");
  callback(error);
});

What is the need to kill streams and remove the close event in the stream being written? If I do not, what could happen?

Thank.

+4
source share

Source: https://habr.com/ru/post/1673615/


All Articles