I'm studing RxJS; I don't understand why mergeMap and switchMap give me the same result; the following source code comes from https://codeburst.io/rxjs-by-example-part-2-8c6eda15bd7f with my little modification (use of new Observable and myObservable part):
import { of, Observable, Observer } from 'rxjs';
import { map, mergeAll, delay, switchAll, switchMap, mergeMap, bufferCount, filter } from 'rxjs/operators';
const myObservable = new Observable((observer) => {
observer.next(1);
observer.next(2);
observer.next(3);
observer.next(4);
});
const multiplyObservable = myObservable.pipe(map((o: any) => o * 2));
const filterObservable = myObservable.pipe(filter((o: any) => o < 3));
console.log('MY_OBSERVABLE');
myObservable.subscribe(o => console.log(o));
console.log('MULTIPLY_OBSERVABLE');5
multiplyObservable.subscribe(o => console.log(o));
console.log('FILTER_OBSERVABLE');
filterObservable.subscribe(o => console.log(o));
const myAllObservable=myObservable.pipe(
map((o: any) => o * 2),
filter((o: any) => o < 5),
switchMap((o: any)=> of(o+10))
// mergeMap((o: any)=> of(o+10))
);
console.log('ALL_OBSERVABLE');
myAllObservable.subscribe(o => console.log(`myAllObservable: ${o}.`));
If I comment switchMap and execute mergeMap, the result is the same? Why?