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 )

//emit value every 1 second
const source = Rx.Observable.interval(1000);
//throttle for 2 seconds, emit latest value
const example = source.throttle(val => Rx.Observable.interval(2000));
//output: 0...3...6...9
const subscribe = example.subscribe(val => console.log(val));
Example 2: Throttle with promise

( jsBin | jsFiddle )

//emit value every 1 second
const source = Rx.Observable.interval(1000);
//incrementally increase the time to resolve based on source
const promise = val => new Promise(resolve => setTimeout(() => resolve(`Resolved: ${val}`), val * 100));
//when promise resolves emit item from source
const example = source
    .throttle(promise)
  .map(val => `Throttled off Promise: ${val}`);

const subscribe = example.subscribe(val => console.log(val));

Additional Resources


:file_folder: Source Code: https://github.com/ReactiveX/rxjs/blob/master/src/operator/throttle.ts

results matching ""

    No results matching ""