I try to have access to the value emitted by the initial Observable after switchMap, inside the map operator.
I have looked at other operators, but i didn't find out any solution.
Here's an operator for RxJS that will emit both the outer value and inner value as an array pair [outer, inner]
.
Here's a switchMap()
variant.
export function withSwitchMap<T, R>(inner: (T) => Observable<R>): OperatorFunction<T, [T, R]> {
return (source: Observable<T>): Observable<[T, R]> => {
return source.pipe(
switchMap(a => inner(a).pipe(map(b => [a, b] as [T, R])))
);
}
}
Here's a mergeMap()
variant.
export function withMergeMap<T, R>(inner: (T) => Observable<R>): OperatorFunction<T, [T, R]> {
return (source: Observable<T>): Observable<[T, R]> => {
return source.pipe(
mergeMap(a => inner(a).pipe(map(b => [a, b] as [T, R])))
);
}
}