Attaches an AbortSignal to a readable or writeable stream. This lets code
control stream destruction using an AbortController.
Calling abort on the AbortController corresponding to the passed AbortSignal will behave the same way as calling .destroy(new AbortError()) on the
stream, and controller.error(new AbortError()) for webstreams.
importfsfrom'node:fs';
constcontroller = newAbortController(); constread = addAbortSignal( controller.signal, fs.createReadStream(('object.json')), ); // Later, abort the operation closing the stream controller.abort();
Or using an AbortSignal with a readable stream as an async iterable:
constcontroller = newAbortController(); setTimeout(() =>controller.abort(), 10_000); // set a timeout conststream = addAbortSignal( controller.signal, fs.createReadStream(('object.json')), ); (async () => { try { forawait (constchunkofstream) { awaitprocess(chunk); } } catch (e) { if (e.name === 'AbortError') { // The operation was cancelled } else { throwe; } } })();
A stream to attach a signal to.
Attaches an AbortSignal to a readable or writeable stream. This lets code control stream destruction using an
AbortController
.Calling
abort
on theAbortController
corresponding to the passedAbortSignal
will behave the same way as calling.destroy(new AbortError())
on the stream, andcontroller.error(new AbortError())
for webstreams.Or using an
AbortSignal
with a readable stream as an async iterable:Or using an
AbortSignal
with a ReadableStream: