Rxjs Angular switchMap


switchmap -> 각각의 input을 Observable로 방출하는 operator

  • 중첩된 service subscribe를 switchMap으로 대체 할수 있다.
  • subscribe callback hell 방지.
constructor(private 1service:1Service, private 2service:2Service){
}

ngOnInit(){
    this.1service.method().subscribe((res)=>{
        this.2service.method(res.id).subscribe((res)=>{
            
        })
    })
}
constructor(private 1service:1Service, private 2service:2Service){
}

ngOnInit(){
    this.1service.method().pipe(switchMap((res)=>this.2service.method(res.id))).subscribe((res)=>{

    })
}