Skip to content

Instantly share code, notes, and snippets.

@miladvafaeifard
Created June 3, 2019 09:17
Show Gist options
  • Save miladvafaeifard/d40e97f20c8a1d298afb1ffc172f3a5f to your computer and use it in GitHub Desktop.
Save miladvafaeifard/d40e97f20c8a1d298afb1ffc172f3a5f to your computer and use it in GitHub Desktop.
Skipping requests in rxjs and Angular, if we enter a white-space, the request gets made again requests run as soon as the model gets changed if the user types something before the request has finished, the request keeps running
import {
debounceTime,
distinctUntilChanged, filter,
map,
switchMap
} from 'rxjs/operators';
//...
this.searchResult$ = this.queries$.pipe(
map((query: string) => query ? query.trim() : ''),
filter(Boolean),
debounceTime(500),
distinctUntilChanged(),
switchMap((query: string) => this.fetchRepositories(query))
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment