Search code examples
rxjsrxjs7

rxjs debouncetime not working when source stream is `of`


Considering the following setup

of('x')
   .pipe(
     startWith('y'),
     debounceTime(1000))
.subscribe(console.log);

DEMO

It prints without any delay the valye 'x'

Doing the same thing as follows

const clicks = fromEvent(document, 'click');
clicks
    .pipe(
       map(() => 'x'),
       startWith('y'),
       debounceTime(1000)
)
.subscribe(console.log);

DEMO

I do get that delay for the inital value y and also later for each click. So the question is, why do I not get any delay when using of?


Solution

  • of() completes immediately after emitting its only value. In this case, debounceTime() does not wait, but also emits right away. From the docs:

    If the completing event happens during dueTime the last cached notification is emitted before the completion event is forwarded to the output observable

    fromEvent() does not complete, so the behavior is as you would expect.