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

Observable: takeUntil() method

· 52CE 我爱测

The **`takeUntil()`** method of the Observable interface returns a new observable that emits values from the source observable until another observable emits a value or errors.

Observable API

The takeUntil() method of the Observable interface returns a new observable that emits values from the source observable until another observable emits a value or errors.

Syntax

takeUntil(value)

Parameters

  • value
    • : A value that can be converted to an observable by Observable.from_static: an Observable, a Promise, an iterable object, or an async iterable object. The converted observable acts as the notifier that determines when to stop emitting source values.

Return value

A new Observable. When subscribed to, it emits values from the source observable until the notifier emits a value or errors, then completes and unsubscribes from both observables. If the source completes or errors first, that notification is forwarded and the notifier is unsubscribed from.

Exceptions

  • TypeError
    • : Thrown if value cannot be converted to an observable.

Description

Like other observable-returning operators, this method is lazy: calling it creates a new observable without subscribing to the source. Processing starts when the returned observable is subscribed to.

The notifier is subscribed to before the source. If it emits a value or errors synchronously, the returned observable completes without subscribing to the source. If the notifier completes without emitting a value, the source continues uninterrupted.

A notifier error completes the returned observable successfully; it is not forwarded as an error.

You can technically pass a synchronous iterable as the value, but the converted observable either never emits if the iterable is empty (and takeUntil() never unsubscribes), or it immediately emits if the iterable is non-empty (and takeUntil() immediately unsubscribes).

Examples

Using takeUntil()

This example displays the mouse coordinates when the pointer moves over either of two <div> elements. Clicking anywhere in the example completes the observable and stops coordinate reporting. Click Restart after the stream ends to try again.

<div></div>
<div></div>
<p></p>
<button id="restart" disabled>Restart</button>
div {
  height: 120px;
  background-color: purple;
  margin-bottom: 40px;
}
const outputElem = document.querySelector("p");

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

function start() {
  restart.disabled = true;
  outputElem.textContent = "Move the mouse";
  document.body
    .when("mousemove")
    .filter((e) => e.target.matches("div"))
    .map((e) => ({ x: e.clientX, y: e.clientY }))
    .takeUntil(
      document.body.when("click").filter((event) => event.target !== restart),
    )
    .subscribe({
      next: reportCoords,
      complete() {
        restart.disabled = false;
      },
    });

  function reportCoords(e) {
    outputElem.textContent = `${e.x},${e.y}`;
  }
}

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

basic-takeUntil

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

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

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