Learn RxJS
Search…
throttle

signature: throttle(durationSelector: function(value): Observable | Promise): Observable

Emit value on the leading edge of an interval, but suppress new values until durationSelector has completed.

Examples

Example 1: Throttle for 2 seconds, based on second observable
1
// RxJS v6+
2
import { interval } from 'rxjs';
3
import { throttle } from 'rxjs/operators';
4
5
//emit value every 1 second
6
const source = interval(1000);
7
//throttle for 2 seconds, emit latest value
8
const example = source.pipe(throttle(val => interval(2000)));
9
//output: 0...3...6...9
10
const subscribe = example.subscribe(val => console.log(val));
Copied!
Example 2: Throttle with promise
1
// RxJS v6+
2
import { interval } from 'rxjs';
3
import { throttle, map } from 'rxjs/operators';
4
5
//emit value every 1 second
6
const source = interval(1000);
7
//incrementally increase the time to resolve based on source
8
const promise = val =>
9
new Promise(resolve =>
10
setTimeout(() => resolve(`Resolved: ${val}`), val * 100)
11
);
12
//when promise resolves emit item from source
13
const example = source.pipe(
14
throttle(promise),
15
map(val => `Throttled off Promise: ${val}`)
16
);
17
18
const subscribe = example.subscribe(val => console.log(val));
Copied!

Additional Resources