how to use switchMap to cancel pending http requests and taking the last subscribe only?

后端 未结 3 1585
礼貌的吻别
礼貌的吻别 2021-01-19 05:11

i tried canceling pending http request using subscription.unsubsribe like this:

getAgentList(pageNumber: number, filter: string): any {
   let requestUrl: s         


        
3条回答
  •  我在风中等你
    2021-01-19 05:41

    basic example:

    export class MyComponent{
        private $filter: Subject = new Subject();
    
        constructor(){
            this.$filter
              .switchMap(filter => this.backEndCommService.getData(filter + this.baseUrl)
              .subscribe(res => {//do something})
        }
    
        getAgentList(filterValue: string){
            this.$filter.next(filterValue);
        }
    
    }
    

    To use switchmap to cancel previous request we need a hot observable which outputs our filter value. We use a subject for that. Everytime we get a new value from somewhere? we push it to the subject.

提交回复
热议问题