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

Observable: every() method

· 52CE 我爱测

The **`every()`** method of the Observable interface returns a promise that fulfills with a boolean indicating whether every value emitted by the source observable satisfies the provided testing function.

Observable API

The every() method of the Observable interface returns a promise that fulfills with a boolean indicating whether every value emitted by the source observable satisfies the provided testing function.

Syntax

every(predicate)
every(predicate, options)

Parameters

  • predicate
    • : A function to execute for each value emitted by the source observable. It should return a truthy value to indicate the value passes the test, and a falsy value otherwise. The function is called with the following arguments:
      • value
        • : The current value being processed.
      • index
        • : The index of the current value being processed, starting from 0.
  • options
    • : An options object containing the following properties:
      • signal
        • : An AbortSignal that can be used to cancel the operation. Aborting the signal unsubscribes from the source and rejects the promise with the signal's AbortSignal.reason. If the signal is already aborted, the promise rejects without subscribing to the source.

Return value

A Promise that fulfills with false as soon as predicate returns a falsy value, or true when the source completes without any value failing the test. If the source completes without emitting any values, the promise fulfills with true.

If the source errors or predicate throws an exception, the promise rejects with that error. If the operation is aborted, the promise rejects with the abort reason.

Description

Like other promise-returning operators, this method subscribes to the source immediately when called. It does not require a separate call to Observable.subscribe.

When predicate returns a falsy value, every() unsubscribes from the source without waiting for it to complete. If the source never completes and all values pass the test, the promise remains pending unless the operation errors or is aborted.

The return value of predicate is converted to a boolean without awaiting it. An async function returns a truthy promise object regardless of its eventual result, so it cannot be used as an asynchronous test.

If predicate throws an exception, the operation unsubscribes from the source.

Examples

Using every()

This example checks whether Shift is held for each of the first three button clicks. It reports false as soon as a click fails the test, or true after all three pass. Click Restart after the stream ends to try again.

<button>Click me</button>
<p>Waiting for clicks</p>
<button id="restart" disabled>Restart</button>
const btn = document.querySelector("button");
const output = document.querySelector("p");

const restart = document.querySelector("#restart");

function start() {
  restart.disabled = true;
  output.textContent = "Waiting for clicks";
  btn
    .when("click")
    .take(3)
    .every((event) => event.shiftKey)
    .then((result) => {
      restart.disabled = false;
      output.textContent = result;
    });
}

restart.when("click").subscribe(start);
start();

basic-every

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

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

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