DEV Community

Deborah Kurata
Deborah Kurata

Posted on

3 1

Higher-Order Mapping Operators

Higher-order mapping operators are used to map inner Observables. They include:

  • concatMap
  • mergeMap
  • switchMap

image

Each higher-order mapping operator:

  • Automatically subscribes to the inner Observable
  • Flattens the resulting Observable, returning Observable<T> instead of Observable<Observable<T>>
  • Automatically unsubscribe from the inner Observable

For example:

products$ = this.categorySelected$
  .pipe(
       switchMap(catId=>
            this.http.get<Product[]>(`${this.url}?cat=${catId}`))
  );

Enter fullscreen mode Exit fullscreen mode

In this code, every time the user selects a new category, this.categorySelected$ emits the id of the selected category. That id is piped through a higher-order mapping operator (switchMap in this case). The switchMap automatically subscribes to the inner Observable, flattens the result (emitting an array of type Product[] instead of Observable<Product[]>), and unsubscribes from the inner Observable.

Additional posts in this series look more closely at several higher-order mapping operators and when to use which.

Image of Timescale

πŸ“Š Benchmarking Databases for Real-Time Analytics Applications

Benchmarking Timescale, Clickhouse, Postgres, MySQL, MongoDB, and DuckDB for real-time analytics. Introducing RTABench πŸš€

Read full post β†’

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

πŸ‘‹ Kindness is contagious

Explore a trove of insights in this engaging article, celebrated within our welcoming DEV Community. Developers from every background are invited to join and enhance our shared wisdom.

A genuine "thank you" can truly uplift someone’s day. Feel free to express your gratitude in the comments below!

On DEV, our collective exchange of knowledge lightens the road ahead and strengthens our community bonds. Found something valuable here? A small thank you to the author can make a big difference.

Okay