Streams

stream.addAbortSignal()

Connects an abort signal to a stream so cancellation destroys or errors the associated stream. Cancellation is a failure path to handle, not successful delivery of remaining data. Tested on Node.js 22.16.0. Example output (one line per entry): ["AbortError true"].

Formal Syntax & Structure

nodejs syntaxnodejs
addAbortSignal(signal, stream)

Parameters & Attributes

NameTypeDescription
signalAbortSignalCancellation source.
streamStreamStream to associate.

Return Value

Stream

The associated stream.

Working Example

Example Snippetnodejs
import { PassThrough, addAbortSignal } from 'node:stream';import { finished } from 'node:stream/promises';const c = new AbortController();const s = addAbortSignal(c.signal, new PassThrough());const done = finished(s, { cleanup: true });c.abort();try {    await done;}catch (e) {    console.log(e.name, s.destroyed);}
Node.js Reference