Request: signal 屬性

Baseline 已廣泛支援

此功能已成熟,並可在多種裝置和瀏覽器版本上執行。自 2019 年 3 月以來,它已在所有瀏覽器中可用。

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

Request 介面的只讀屬性 signal 返回與該請求相關聯的 AbortSignal

一個 AbortSignal 物件。

示例

js
// Create a new abort controller
const controller = new AbortController();

// Create a request with this controller's AbortSignal object
const req = new Request("/", { signal: controller.signal });

// Add an event handler logging a message in case of abort
req.signal.addEventListener("abort", () => {
  console.log("abort");
});

// In case of abort, log the AbortSignal reason, if any
fetch(req).catch(() => {
  if (req.signal.aborted) {
    if (req.signal.reason) {
      console.log(`Request aborted with reason: ${req.signal.reason}`);
    } else {
      console.log("Request aborted but no reason was given.");
    }
  } else {
    console.log("Request not aborted, but terminated abnormally.");
  }
});

// Actually abort the request
controller.abort();

規範

規範
Fetch
# ref-for-dom-request-signal②

瀏覽器相容性