Skip to content

Instantly share code, notes, and snippets.

@belachkar
Last active November 12, 2021 07:15
Show Gist options
  • Save belachkar/64ad6bf906e80aea0c4b028b978912eb to your computer and use it in GitHub Desktop.
Save belachkar/64ad6bf906e80aea0c4b028b978912eb to your computer and use it in GitHub Desktop.

RxJS - common use case

results: Observable<RedditResult[]>;
search: FormControl = new FormControl('');

constructor(ris: RedditImageSearchService) {
  this.results = this.search.valueChanges.pipe(
    map(search => search.trim()),
    debounceTime(200),
    distinctUntilChanged(),
    filter(search => search !== ''),
    switchMap(search => 
      ris.search(search).pipe(
        retry(3),
        startWith([])
      )
    )
  )
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment