Search code examples
rxjsrxjs6

concatMap with interval is not working as expected


I'm trying to understand why my code is not working what I'm expecting it to behave.

If you copy and paste the following code in https://stackblitz.com, you will see it wait 4 seconds then it displays 'aaaa' every second instead of 'bbbb' every second. Why?

import { from, of, race, timer, interval } from 'rxjs';
import { groupBy, mergeMap, toArray, map,merge, reduce, concatMap, delay, concat, timeout, catchError, take } from 'rxjs/operators';

const obs$ = interval(4000).pipe(map(() => 'aaaa'));
const obs2$ = interval(1000).pipe(map(() => 'bbbb'));
 
const result$ = obs$.pipe(concatMap(() => obs2$));
 
const subscribe = obs$.subscribe(val => console.log(val + ' ' + new Date().toLocaleTimeString()));  

Solution

  • import { from, of, race, timer, interval } from 'rxjs';
    import { groupBy, mergeMap, toArray, map,merge, reduce, concatMap, delay, concat, timeout, catchError, take } from 'rxjs/operators';
    
    
    const obs$ = interval(14000).pipe(map(() => 'aaaa'), take(5));
    const obs2$ = interval(1000).pipe(map(() => 'bbbb'));
    
    const result$ = obs$.pipe(concatMap(() => obs2$));
    
    const subscribe = result$.subscribe(val => console.log(val + ' ' + new Date().toLocaleTimeString()));