switchMap hủy request trước và lấy request mới nhất, phù hợp search/autocomplete.
Ví dụ search box:
typescript
results$ = this.search.valueChanges.pipe(
debounceTime(300),
distinctUntilChanged(),
switchMap(term => this.http.get<SearchResult[]>("/api/search", { params: { q: term } })),
)mergeMap chạy song song, concatMap xếp hàng giữ thứ tự, exhaustMap bỏ qua trigger mới khi request cũ đang chạy.
Chọn operator theo concurrency semantics.
switchMap cancels the previous request and keeps the latest, fitting search/autocomplete.
Search box example:
typescript
results$ = this.search.valueChanges.pipe(
debounceTime(300),
distinctUntilChanged(),
switchMap(term => this.http.get<SearchResult[]>("/api/search", { params: { q: term } })),
)mergeMap runs concurrently, concatMap queues in order, and exhaustMap ignores new triggers while the previous request is running.
Choose by concurrency semantics.