How to use map operator as mergeMap?
Asked Answered
O

2

8

Now that mergeMap is deprecated, and the source has this comment in it:

/* @deprecated resultSelector no longer supported, use inner map instead */

How do I use an "inner map" instead? I guess that means using the map operator function inside of .pipe, but the observable is not flattened, as it is with mergeMap.

obs1$.pipe(map(() => obs2$)).subscribe(r => console.log(r === obs2$))
// > true

So, how do the equivalent of mergeMap without it?

Olympiaolympiad answered 11/6, 2018 at 23:57 Comment(0)
C
12

You still use mergeMap, it's just the resultSelector function that deprecates.

This one is not deprecated:

export function mergeMap<T, R>(project: (value: T, index: number) => ObservableInput<R>, concurrent?: number): OperatorFunction<T, R>;

However, these are:

/** @deprecated resultSelector no longer supported, use inner map instead */
export function mergeMap<T, R>(project: (value: T, index: number) => ObservableInput<R>, resultSelector: undefined, concurrent?: number): OperatorFunction<T, R>;
/** @deprecated resultSelector no longer supported, use inner map instead */
export function mergeMap<T, I, R>(project: (value: T, index: number) => ObservableInput<I>, resultSelector: (outerValue: T, innerValue: I, outerIndex: number, innerIndex: number) => R, concurrent?: number): OperatorFunction<T, R>;
Crampon answered 12/6, 2018 at 0:36 Comment(4)
And the workaround for the depreciation of the result selector is in the migration guide: github.com/ReactiveX/rxjs/blob/master/docs_app/content/guide/v6/…Unfledged
If anyone are looking for the migration doc that @Unfledged posted, it was deleted and can be found here: github.com/ReactiveX/rxjs/commit/…Isopropyl
@Unfledged the link doesn't work. is there another code example enywhere?Adornment
@Andresch This link should be good and shouldn't break: github.com/ReactiveX/rxjs/blob/…Unfledged
P
0

Here's example of doing this inner map

this.subj1.pipe(mergeMap((outer) => this.subj2.pipe(map((inner) => [outer, inner]))))
Photic answered 2/6, 2022 at 11:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.