W3cubDocs

/RxJS

repeat

function stable

Returns an Observable that will resubscribe to the source stream when the source stream completes, at most count times.

repeat<T>(count: number = -1): MonoTypeOperatorFunction<T>

Parameters

count

Optional. Default is -1.

The number of times the source Observable items are repeated, a count of 0 will yield an empty Observable.

Returns

MonoTypeOperatorFunction<T>: An Observable that will resubscribe to the source stream when the source stream completes , at most count times.

Description

Repeats all values emitted on the source. It's like retry, but for non error cases.

repeat marble diagram

Similar to retry, this operator repeats the stream of items emitted by the source for non error cases. Repeat can be useful for creating observables that are meant to have some repeated pattern or rhythm.

Note: repeat(0) returns an empty observable and repeat() will repeat forever

Example

Repeat a message stream

import { of } from 'rxjs';
import { repeat, delay } from 'rxjs/operators';

const source = of('Repeat message');
const example = source.pipe(repeat(3));
example.subscribe(x => console.log(x));

// Results
// Repeat message
// Repeat message
// Repeat message

Repeat 3 values, 2 times

import { interval } from 'rxjs';
import { repeat, take } from 'rxjs/operators';

const source = interval(1000);
const example = source.pipe(take(3), repeat(2));
example.subscribe(x => console.log(x));

// Results every second
// 0
// 1
// 2
// 0
// 1
// 2

See Also

© 2015–2018 Google, Inc., Netflix, Inc., Microsoft Corp. and contributors.
Code licensed under an Apache-2.0 License. Documentation licensed under CC BY 4.0.
https://rxjs.dev/api/operators/repeat