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

Observable: flatMap() method

· 52CE 我爱测

The **`flatMap()`** method of the Observable interface returns a new observable that maps each value of the source observable to an inner observable and emits the inner observables' values sequentially.

Observable API

The flatMap() method of the Observable interface returns a new observable that maps each value of the source observable to an inner observable and emits the inner observables' values sequentially.

Syntax

flatMap(mapper)

Parameters

  • mapper
    • : A function to execute for each value emitted by the source observable. 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 arguments:
      • value
        • : The current value being processed.
      • index
        • : The index of the current value being processed, starting from 0.

Return value

A new Observable. When subscribed to, it calls mapper for each source value, converts the return value to an observable, and emits that inner observable's values. It waits for the current inner observable to complete before calling mapper for the next source value. The returned observable completes after the source and all inner observables have completed.

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.

Source values that arrive while an inner observable is active are queued. If that inner observable never completes, later source values remain queued and their mapper calls do not run. To unsubscribe from the current inner observable when a new source value arrives, use Observable.switchMap instead.

If mapper throws an exception or its return value cannot be converted to an observable, the returned observable errors. Errors from the source or an inner observable are also forwarded. In each case, the returned observable unsubscribes from its source and any active inner observable.

Examples

Using flatMap()

This example displays mouse coordinates while dragging from a <div> element. Each mouse press starts an inner stream of mouse movements that ends when the mouse button is released. flatMap() forwards those movements and waits for the current inner stream to complete before processing another mouse press.

<div>Press here and drag.</div>
<p>Waiting for a drag</p>
div {
  height: 120px;
  background-color: lavender;
  user-select: none;
}
const target = document.querySelector("div");
const output = document.querySelector("p");

target
  .when("mousedown")
  .flatMap(() => document.when("mousemove").takeUntil(document.when("mouseup")))
  .subscribe((event) => {
    output.textContent = `${event.clientX},${event.clientY}`;
  });

basic-flatMap

For a more complete example, see Canvas drawing.

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

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

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