WritableStreamDefaultController: signal property

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since March 2023.

Note: This feature is available in Web Workers.

The read-only signal property of the WritableStreamDefaultController interface returns the AbortSignal associated with the controller.

Value

An AbortSignal object.

Examples

Aborting a long write operation

In this example, we simulate a slow operation using a local sink: We do nothing when some data is written but to wait for a second. This gives us enough time to call the writer.abort() method and to immediately reject the promise.

js
const writingStream = new WritableStream({
  // Define the slow local sink to simulate a long operation
  write(chunk, controller) {
    return new Promise((resolve, reject) => {
      controller.signal.addEventListener("abort", () =>
        reject(controller.signal.reason),
      );

      // Do nothing but wait with the data: it is a local sink
      setTimeout(resolve, 1000); // Timeout to simulate a slow operation
    });
  },
});

// Perform the write
const writer = writingStream.getWriter();
writer.write("Lorem ipsum test data");

// Abort the write manually
await writer.abort("Manual abort!");

Transferring the AbortSignal to the underlying layer

In this example, we use the Fetch API to actually send the message to a server. The Fetch API also support AbortSignal: It is possible to use the same object for both the fetch method and the WritableStreamDefaultController.

js
const endpoint = "https://www.example.com/api"; // Fake URL for example purpose
const writingStream = new WritableStream({
  async write(chunk, controller) {
    // Write to the server using the Fetch API
    const response = await fetch(endpoint, {
      signal: controller.signal, // We use the same object for both fetch and controller
      method: "POST",
      body: chunk,
    });
    await response.text();
  },
});

// Perform the write
const writer = writingStream.getWriter();
writer.write("Lorem ipsum test data");

// Abort the write manually
await writer.abort("Manual abort!");

Specifications

Specification
Streams
# ref-for-ws-default-controller-signal①

Browser compatibility

desktop mobile server
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
Deno
Node.js
signal
Chrome – Full support
Chrome 98 (Release date: 2022-02-01)
footnote Full support
Edge – Full support
Edge 98 (Release date: 2022-02-03)
footnote Full support
Firefox – Full support
Firefox 100 (Release date: 2022-05-03)
footnote Full support
Opera – Full support
Opera 84 (Release date: 2022-02-16)
footnote Full support
Safari – Full support
Safari 16.4 (Release date: 2023-03-27)
footnote Full support
Chrome Android – Full support
Chrome Android 98 (Release date: 2022-02-01)
footnote Full support
Firefox for Android – Full support
Firefox for Android 100 (Release date: 2022-05-03)
footnote Full support
Opera Android – Full support
Opera Android 68 (Release date: 2022-03-30)
footnote Full support
Safari on iOS – Full support
Safari on iOS 16.4 (Release date: 2023-03-27)
footnote Full support
Samsung Internet – Full support
Samsung Internet 18 (Release date: 2022-08-08)
footnote Full support
WebView Android – Full support
WebView Android 98 (Release date: 2022-02-01)
footnote Full support
WebView on iOS – Full support
WebView on iOS 16.4 (Release date: 2023-03-27)
footnote Full support
Deno – Full support
Deno 1.16 (Release date: 2021-11-08)
footnote Full support
Node.js – Full support
Node.js 16.5 (Release date: 2021-07-14)
footnote Full support

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support