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

Observable: finally() method

· 52CE 我爱测

The **`finally()`** method of the Observable interface returns a new observable that mirrors the source observable and calls a callback when its subscription ends.

Observable API

The finally() method of the Observable interface returns a new observable that mirrors the source observable and calls a callback when its subscription ends.

Syntax

finally(callback)

Parameters

  • callback
    • : A function to execute when the subscription ends through completion, an error, or all observers unsubscribing. The function is called without arguments. Its return value is ignored.

Return value

A new Observable. When subscribed to, it emits the source observable's values and forwards its completion or error, running callback when the subscription ends.

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 callback is registered as a teardown on the returned observable's Subscriber. It runs synchronously before the observers' completion or error callbacks, and also runs if all observers unsubscribe. For details on teardown behavior, see Subscriber.addTeardown().

[!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.

If callback throws an exception, it is reported to the global object without changing the stream's completion or error. A returned promise is not awaited, and its rejection is not handled by finally().

Examples

Using finally()

This example displays mouse coordinates until the Stop button is clicked. The finally() callback adds a message when coordinate reporting ends. Click Restart after the stream ends to try again.

<button>Stop</button>
<p>Move the mouse</p>
<button id="restart" disabled>Restart</button>
html {
  height: 100%;
}

body {
  box-sizing: border-box;
  min-height: 100%;
  margin: 0;
  padding: 8px;
}
const btn = document.querySelector("button");
const output = document.querySelector("p");

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

function start() {
  restart.disabled = true;
  output.textContent = "Move the mouse";
  document.body
    .when("mousemove")
    .takeUntil(btn.when("click"))
    .finally(() => {
      restart.disabled = false;
      output.textContent += " — Reporting stopped.";
    })
    .subscribe((event) => {
      output.textContent = `${event.clientX},${event.clientY}`;
    });
}

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

basic-finally

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

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

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