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

TaskSignal: any() static method

· 52CE 我爱测

The **`TaskSignal.any()`** static method takes an iterable of AbortSignal objects and returns a TaskSignal. The returned task signal is aborted when any of the abort signals is aborted.

Prioritized Task Scheduling API

The TaskSignal.any() static method takes an iterable of AbortSignal objects and returns a TaskSignal. The returned task signal is aborted when any of the abort signals is aborted.

When the task signal is aborted, its AbortSignal.reason property will be set to the reason of the first signal that is aborted.

Syntax

TaskSignal.any(signals)
TaskSignal.any(signals, init)

Parameters

  • signals
    • : An iterable (such as an Array) of abort signals.
  • init
    • : Contains optional configuration parameters. Currently only one property is defined:
      • priority
        • : One of the following:
          • A priority string which is one of user-blocking, user-visible and background.
          • A TaskSignal.

Return value

A TaskSignal instance. It will be aborted when the first signal passed into signals is aborted. When this happens:

  • Its AbortSignal.reason property will be set to the reason of the signal that caused this signal to abort.

  • Its TaskSignal.priority property will be determined by the priority parameter:

    • If the priority parameter was a string, it will be the value of the string.
    • If the priority parameter was a TaskSignal, it will be the value of that signal's TaskSignal/priority.

Description

The abort-signal cleanup considerations described for AbortSignal/any_static also apply to TaskSignal.any(). There is no method to unsubscribe the returned signal from its input signals, and aborting it does not abort the other input signals or cancel their timeouts.

If init.priority is a signal with a changeable priority, the returned signal is also kept alive while it has a priority source and either prioritychange listeners or internal priority change steps registered by an API. Remove listeners added by your code when they are no longer needed. Aborting the signal does not remove its prioritychange listeners or stop it from following priority changes.

Examples

Using TaskSignal.any()

This example demonstrates combining both a signal from a TaskController, and a timeout signal from AbortSignal/timeout_static.

const cancelDownloadButton = document.getElementById("cancelDownloadButton");

const userCancelController = new TaskController({
  priority: "user-visible",
});

cancelDownloadButton.addEventListener("click", () => {
  userCancelController.abort();
});

// Timeout after 5 minutes
const timeoutSignal = TaskSignal.timeout(1_000 * 60 * 5);

// This signal will abort when either the user clicks the cancel button or 5 minutes is up whichever is sooner
const combinedSignal = TaskSignal.any([
  userCancelController.signal,
  timeoutSignal,
]);

try {
  const res = await fetch(someUrlToDownload, {
    // Stop the fetch when any of the
    signal: combinedSignal,
  });
  const body = await res.blob();
  // Do something with downloaded content
  // …
} catch (e) {
  if (e.name === "AbortError") {
    // Canceled by the user
  } else if (e.name === "TimeoutError") {
    // Show user that download timed out
  } else {
    // Other error, e.g. network error
  }
}

Specifications

Browser compatibility

See also

  • AbortSignal/any_static

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

资料来源与适用范围

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

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

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