skip to content

repeat

 

Repeat operator will resubscribe to source once it completes. repeat takes optional number of repeats, if omitted — will resubscribe indefinitely, if set to 0 — will return an empty observable.

Basic example

1
2
3
4
5
6
7
8
9
10
11
const { rxObserver } = require('api/v0.3');
const { timer } = require('rxjs');
const { repeat } = require('rxjs/operators');

timer(5)
  .pipe(
    repeat(5)
  )
  .subscribe(rxObserver());

0msstartcomplete00 00 00 00 00

Smarter source

When source stream completes, repeat will re-subscribe again and will get a new source stream:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
const { rxObserver } = require('api/v0.3');
const { timer, iif, of } = require('rxjs');
const { repeat, delay } = require('rxjs/operators');

iif(
  () => Date.now() < 10
  , of('0..10')
  , of('10+')
)
  .pipe(
    delay(5),
    repeat(5)
  )
  .subscribe(rxObserver());

0msstartcomplete0..100..10 0..100..10 10+10+ 10+10+ 10+10+