Created
June 3, 2019 09:17
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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