时间间隔未按预期运行的Concatmap

时间:2018-07-01 19:59:34

标签: rxjs rxjs6

我试图理解为什么我的代码无法正常工作。

如果您将以下代码复制粘贴到https://stackblitz.com中,则会看到它等待4秒钟,然后每秒显示“ aaaa”,而不是每秒显示“ bbbb”。为什么?

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

1 个答案:

答案 0 :(得分:0)

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