All you need to know about the auditTime operator in RxJS

Satyapriya Mishra
2 min readAug 22, 2020

--

The auditTime operator waits for a given duration of time and then emits the most recent value emitted by the source observable. This process is repeated again and again till the source observable emitting the values is complete.

Signature

auditTime(duration: number, scheduler: SchedulerLike = async): MonoTypeOperatorFunction

duration: Time in milliseconds for which the values from the source observable are ignored.

scheduler: It manages the timer that rate limits the emissions to the output observable.Default value is async.

MonoTypeOperatorFunction: It is an observable that does the rate limiting emission from the source observable.

A simple code block for the auditTime operator is given below.

const source = interval( 1000 ).pipe(take( 20 )); // (1)const result = source.pipe(auditTime( 3000 )); // (2)result.subscribe(val => console.log(val)); // (3)

Let’s analyze the above code block in steps.

In step1, we create an observable through the interval operator which emits values in certain durations and we decided to take only the first 20 values , through the take operator. In the next step, we pipe that source observable and pass in the auditTime operator with 3000ms as the duration time. Next step we subscribe to the observable and print the emitted values.One thing to observe here is that the audit operator takes the duration specifier as an observable, but the auditTime operator takes it as a number.

The output of the above code comes as numbers emitted after 3ms duration which is very evident.

// 3// 7// 11// 15

This is all about the basics of the auditTime operator.

--

--

Satyapriya Mishra
Satyapriya Mishra

No responses yet