W3cubDocs

/Web APIs

WritableStreamDefaultController: signal property

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(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

Browser compatibility

Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari WebView Android Chrome Android Firefox for Android Opera Android Safari on IOS Samsung Internet
signal 98 98 100 No 84 16.4 98 98 100 68 16.4 18.0

© 2005–2023 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/WritableStreamDefaultController/signal