Ignore switchMap return value

Ignore switchMap return value



I want to resolve an observable but I don't want the return value to replace the previous value in the pipe. Is there any asynchronous tap()? I need an operator like a switchMap but I want to ignore the return.


tap()


switchMap


of(1).pipe(switchMap(() => of(2))).subscribe(console.log); // expected: 1



I could create a custom operator but sure there's something built-in in rxjs.





of(1).pipe(switchMap(value => concat(of(2).pipe(ignoreElements()), of(value)))) which could easily be turned into a user-land pipeable operator.
– cartant
Feb 23 at 4:11



of(1).pipe(switchMap(value => concat(of(2).pipe(ignoreElements()), of(value))))




2 Answers
2



I ended up with this custom operator. It is like tap but resolves observables (and should be updated to also support promises).


export function switchTap<T, R>(next: (x: T) => Observable<R>): MonoTypeOperatorFunction<T>;
export function switchTap<R>(observable: Observable<R>): MonoTypeOperatorFunction<R>;
export function switchTap<T, R>(
arg: Observable<T> | ((x: T) => Observable<R>)
): MonoTypeOperatorFunction<T>
const next: (x: any) => Observable<T



Usage:


of(1).pipe(switchTap(of(2))).subscribe(console.log) // 1



or with a function:


of(1)
.pipe(
switchTap(value =>
console.log(value); // value: 1
return of(value + 1);
)
)
.subscribe(console.log); // 1





I think maybe your naming of 'switchTap' is confusing me (not that that is anything new with RxJs!). If you get a chance could you add an explanation of what this is actually for and a plain english description? Is the idea to 'wait' for the observable in the switchTap to complete (allowing it to receive the current value of whatever is in the pipe) and then continue. Maybe your original asyncTap is a better name if that's what it is doing? Also curious what you (or others) are actually using this for.
– Simon_Weaver
Aug 9 at 4:41



If you just want to simply ignore the values of the subscribe, then just don't pass in any arguments in the subscribe callback:


subscribe


of(1).pipe(switchMap(() => of(2))).subscribe(()=>
console.log('no arguments')
);



If you however want to retain the values of the first observable, things can get tricky. One way is to use Subject to retain the value:


Subject


//create a BehaviorSubject
var cache = new BehaviorSubject<any>(0);

of(1).pipe(switchMap((first) =>
cache.next(first);
return of(2);
)).subscribe(() =>
console.log(cache.value) //gives 1
);



Or you can use .map() to alter the values. This is kind of hacky and the code is harder to maintain:


.map()


of(1).pipe(switchMap((first) =>
return of(2).map(() => first);
)).subscribe((second) =>
console.log(second) //gives 1 because the values was mapped
);






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)