throttle
signature: throttle(durationSelector: function(value): Observable | Promise): Observable
Emit value only when duration, determined by provided function, has passed.
Examples
Example 1: Throttle for 2 seconds, based on second observable
( jsBin |
jsFiddle )
import { interval } from 'rxjs/observable/interval';
import { throttle } from 'rxjs/operators';
const source = interval(1000);
const example = source.pipe(throttle(val => interval(2000)));
const subscribe = example.subscribe(val => console.log(val));
Example 2: Throttle with promise
( jsBin |
jsFiddle )
import { interval } from 'rxjs/observable/interval';
import { throttle, map } from 'rxjs/operators';
const source = interval(1000);
const promise = val =>
new Promise(resolve =>
setTimeout(() => resolve(`Resolved: ${val}`), val * 100)
);
const example = source
.pipe(
throttle(promise),
map(val => `Throttled off Promise: ${val}`);
);
const subscribe = example.subscribe(val => console.log(val));
Additional Resources
Source Code:
https://github.com/ReactiveX/rxjs/blob/master/src/internal/operators/throttle.ts