Toan Nguyen
Toan Nguyen

Reputation: 11581

RxJs Debounce does not emit any value

As far as I understand, debounce keeps track of the most recent value from the source Observable, and spawns a duration Observable by calling the durationSelector function

I have the following code

    import { interval } from 'rxjs';
    import { debounce } from 'rxjs/operators';

    const data = interval(1000);

    const silenceTimer = interval(2000);
    data.pipe(debounce(val => silenceTimer)).subscribe(x => console.log(x));

I expect it will emit 1, 3, 5,etc., but it does not emit any value at all. Do I understand the operator correctly or did I do something wrong. Thank you

EDIT: I understand why no value is emitted now! because the interval 2000 is greater than the generation interval 1000, so all values are thrown away. If I reduce the debouncing interval to 500, all values will be emitted

Upvotes: 2

Views: 228

Answers (1)

dmcgrandle
dmcgrandle

Reputation: 6060

From the docs:

debounce() Emits a value from the source Observable only after a particular time span determined by another Observable has passed without another source emission.

Note the phrase: without another source emission. In your case interval will keep generating emissions within the 2000ms of your durationSelector, so debounce() never gets a chance to emit.

Upvotes: 3

Related Questions