How to achieve a debounce service on input keyup event in angular2 with rxjs

So the chain is really correct but the problem is that you’re creating an Observable and subscribe to it on every keyup event. That’s why it prints the same value multiple times. There’re simply multiple subscriptions which is not what you want to do.

There’re obviously more ways to do it correctly, for example:

  selector: 'my-app',
  template: `
      <input type="text" (keyup)='$event)'>
export class App implements OnDestroy {

  public keyUp = new Subject<KeyboardEvent>();

  private subscription: Subscription;

  constructor() {
    this.subscription = this.keyUp.pipe(
      map(event =>,
      mergeMap(search => of(search).pipe(

  ngOnDestroy(): void {

See your updated demo:

Jan 2019: Updated for RxJS 6

Leave a Comment