WritableStreamDefaultController: error() 方法

Baseline 已廣泛支援

此功能已成熟,並可在許多裝置和瀏覽器版本上使用。自 2022 年 5 月以來,它已在各個瀏覽器中可用。

注意:此功能在 Web Workers 中可用。

WritableStreamDefaultController 介面的 error() 方法會導致與關聯流的任何未來互動都出錯。

此方法很少使用,因為通常情況下,從底層 sink 的方法之一返回一個被拒絕的 promise 就足夠了。然而,對於響應底層 sink 正常互動生命週期之外的事件而突然關閉流,它可能很有用。

語法

js
error(message)

引數

message

一個字串,表示你希望未來互動失敗時顯示的錯誤。

返回值

無(undefined)。

異常

TypeError

你試圖出錯的流不是 WritableStream

示例

js
const writableStream = new WritableStream({
  start(controller) {
    // do stuff with controller

    // error stream if necessary
    controller.error("My error is broken");
  },
  write(chunk, controller) {
    // …
  },
  close(controller) {
    // …
  },
  abort(err) {
    // …
  },
});

規範

規範
Streams
# ref-for-ws-default-controller-error①

瀏覽器相容性