throttle
signature: throttle(durationSelector: function(value): Observable | Promise): Observable
throttle(durationSelector: function(value): Observable | Promise): ObservableEmit value on the leading edge of an interval, but suppress new values until durationSelector has completed.
durationSelector has completed.Examples
Example 1: Throttle for 2 seconds, based on second observable
( StackBlitz | jsBin | jsFiddle )
// RxJS v6+
import { interval } from 'rxjs';
import { throttle } from 'rxjs/operators';
//emit value every 1 second
const source = interval(1000);
//throttle for 2 seconds, emit latest value
const example = source.pipe(throttle(val => interval(2000)));
//output: 0...3...6...9
const subscribe = example.subscribe(val => console.log(val));Example 2: Throttle with promise
( StackBlitz | jsBin | jsFiddle )
Additional Resources
:newspaper: - Official docs
Filtering operator: throttle and throttleTime
:video_camera: :dollar: - André Staltz
:file_folder: Source Code: https://github.com/ReactiveX/rxjs/blob/master/src/internal/operators/throttle.ts
Last updated
Was this helpful?