Kevin Beal
Kevin Beal

Reputation: 10849

How to use map operator as mergeMap?

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?

Upvotes: 8

Views: 10836

Answers (2)

starfruit3254
starfruit3254

Reputation: 189

Here's example of doing this inner map

this.subj1.pipe(mergeMap((outer) => this.subj2.pipe(map((inner) => [outer, inner]))))

Upvotes: 0

Ngoc Nam Nguyen
Ngoc Nam Nguyen

Reputation: 513

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>;

Upvotes: 12

Related Questions