大网数据广告活动
Web 标准与前端开发

Subscriber: signal property

· 52CE 我爱测

The **`signal`** read-only property of the Subscriber interface provides an AbortSignal that is aborted when the subscription ends. A producer can pass this signal to APIs such as Window.fetch to cancel work when it is no longer needed.

Observable API

The signal read-only property of the Subscriber interface provides an AbortSignal that is aborted when the subscription ends. A producer can pass this signal to APIs such as Window.fetch to cancel work when it is no longer needed.

Value

An internally created AbortSignal. It is aborted when Subscriber.complete() or Subscriber.error() is called, or when all observers unsubscribe.

Description

This is a different object from any signal passed to Observable.subscribe(). A signal passed to subscribe() controls the observers subscribed with that signal; subscriber.signal tracks the shared subscription. When one observer unsubscribes, subscriber.signal is not aborted if other observers are still subscribed.

[!NOTE] This shared-subscription behavior may change. A proposal to give each observer its own Subscriber would make each subscription start a separate execution instead of reusing an active subscription.

For cleanup that does not use an API accepting an AbortSignal, use Subscriber.addTeardown(). Unlike registering an abort event listener, addTeardown() also runs the callback immediately if the subscriber is already inactive.

Examples

Canceling a fetch request

This example wraps a request for /data.json in an observable. Passing subscriber.signal to fetch() associates the request with the subscription's lifetime.

const observable = new Observable((subscriber) => {
  fetch("/data.json", { signal: subscriber.signal })
    .then((response) => {
      if (!response.ok) {
        throw new Error(`Request failed: ${response.status}`);
      }
      return response.json();
    })
    .then((data) => {
      subscriber.next(data);
      subscriber.complete();
    })
    .catch((error) => {
      if (subscriber.active) {
        subscriber.error(error);
      }
    });
});

const controller = new AbortController();
observable.subscribe(
  {
    next(data) {
      console.log(data);
    },
    error(error) {
      console.error(error);
    },
  },
  { signal: controller.signal },
);

// To cancel the subscription and any pending request:
// controller.abort();

If controller.abort() is called while the request is pending, the only observer unsubscribes, which aborts subscriber.signal and cancels the request. The promise rejection handler checks subscriber.active so that cancellation does not cause a call to error() on an inactive subscriber, which would report an error to the global object.

On success, the producer sends the parsed data and completes the subscription. Request failures are forwarded to the observer's error callback while the subscription is active.

Specifications

Browser compatibility

See also

来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
MDN Web Docs / Mozilla 贡献者
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 2.5

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。