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

Observable: inspect() method

· 52CE 我爱测

The **`inspect()`** method of the Observable interface returns a new observable that mirrors the source observable and calls callbacks to inspect its values and subscription lifecycle.

Observable API

The inspect() method of the Observable interface returns a new observable that mirrors the source observable and calls callbacks to inspect its values and subscription lifecycle.

Syntax

inspect()
inspect(inspector)

Parameters

  • inspector
    • : An object containing any of the following callback functions:

      • next
        • : A function called with each source value before it is forwarded to observers.
      • error
        • : A function called with the source's error before it is forwarded to observers.
      • complete
        • : A function called without arguments when the source completes, before completion is forwarded to observers.
      • subscribe
        • : A function called without arguments when the returned observable's subscription starts, before it subscribes to the source.
      • abort
        • : A function called with the abort reason when all observers unsubscribe from the returned observable. It is not called when the source completes or errors.

      Alternatively, inspector can be a function, which is equivalent to passing an object with that function as its next callback. All callback return values are ignored.

Return value

A new Observable. When subscribed to, it emits the source observable's values and forwards its completion or error, calling the corresponding inspector callbacks before forwarding each notification.

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.

If a subscribe, next, error, or complete callback throws an exception, the returned observable errors with that exception. A thrown exception from subscribe prevents subscription to the source. An exception thrown by abort is reported to the global object.

The callbacks run synchronously. Returned promises are not awaited, and their rejections are not handled by inspect().

Examples

Using inspect()

This example counts the first three button clicks. The inspect() callbacks log the subscription's start, each event before the count is updated, and the final count when the subscription completes. Click Restart after the stream ends to try again.

<button>Click me</button>
<p>Click count: 0</p>
<button id="restart" disabled>Restart</button>
const btn = document.querySelector("button");
const para = document.querySelector("p");

let countValue = 0;

function increment() {
  countValue++;
  para.textContent = `Click count: ${countValue}`;
}

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

function start() {
  restart.disabled = true;
  countValue = 0;
  para.textContent = "Click count: 0";
  btn
    .when("click")
    .take(3)
    .inspect({
      subscribe() {
        console.log(`Subscription started`);
      },
      next(e) {
        console.log(`Count value before click: ${countValue}`);
        console.log(`Event type: ${e.type}`);
      },
      complete() {
        console.log(`Final count value: ${countValue}`);
      },
    })
    .subscribe({
      next: increment,
      complete() {
        para.textContent = `No more clicks!`;
        restart.disabled = false;
      },
    });
}

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

Open the browser's console and click the button three times to see the logged notifications.

basic-inspect

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

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

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