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

Observable: catch() method

· 52CE 我爱测

The **`catch()`** method of the Observable interface returns a new observable that replaces an error from the source observable with values from another observable.

Observable API

The catch() method of the Observable interface returns a new observable that replaces an error from the source observable with values from another observable.

Syntax

catch(callback)

Parameters

  • callback
    • : A function to execute when the source observable errors. It must return 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 function is called with the following argument:
      • error
        • : The error from the source observable.

Return value

A new Observable. When subscribed to, it emits the source observable's values until the source errors, then calls callback and emits values from the observable converted from its return value. It completes when the source completes without an error, or when the replacement observable completes.

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 callback throws an exception or its return value cannot be converted to an observable, the returned observable errors. Errors from the replacement observable are also forwarded; they do not call callback again.

catch() does not resume the source subscription or retry it automatically. The source subscription has already ended when callback runs. To handle an error from one inner observable while continuing to receive source values, place catch() inside the mapper passed to Observable.flatMap or Observable.switchMap.

Examples

Handling errors in an inner observable

This example reports the distance dragged until it exceeds 100 pixels. The error handler ends the current drag's stream while allowing future drags to start.

<div>Press here and move the mouse.</div>
<p>Waiting for a drag</p>
div {
  padding: 20px;
  border: 1px solid;
  user-select: none;
}
const target = document.querySelector("div");
const output = document.querySelector("p");

target
  .when("mousedown")
  .switchMap((start) =>
    document
      .when("mousemove")
      .takeUntil(document.when("mouseup"))
      .map((move) => {
        const distance = Math.hypot(
          move.clientX - start.clientX,
          move.clientY - start.clientY,
        );
        if (distance > 100) {
          throw new Error("Too far! Start a new drag.");
        }
        return distance;
      })
      .catch((error) => {
        output.textContent = error.message;
        return [];
      }),
  )
  .subscribe((distance) => {
    output.textContent = `Distance: ${Math.round(distance)} pixels`;
  });

Placing catch() inside Observable.switchMap keeps the error within the inner stream. The empty array completes that stream without emitting another value.

catch-drag

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

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

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