0

i got this code:

ngOnInit(): void { this.getEmployees(); this.employeesService .reloadEmployees() .subscribe(() => this.getEmployees()); this.employeesService.searchValue$ .subscribe(value => this.employeesService.searchEmployees(value) .subscribe(el => this.allEmployees = el)); } 

Do you guys think that I can simplify that code? What should I do? I need to reduce that subscriptions, because at this point it doesnt look great

2
  • you can use switchMap (learnrxjs.io/learn-rxjs/operators/transformation/switchmap) Commented Feb 4, 2022 at 13:33
  • What's happening in getEmployees ? Normally a getter method returns the thing it's getting, but you're not using that value, so I imagine there's some random side effect in that function you're interested in. What is it and why? Commented Feb 4, 2022 at 18:41

1 Answer 1

1

you should avoid to subscribe inside another subscription. You can sole it like this:

this.employeesService.searchValue$.pipe( concatMap(value => this.employeesService.searchEmployees$(value)) ).subscribe(el => this.allEmployees = el); 
Sign up to request clarification or add additional context in comments.

1 Comment

or maybe use switchMap instead of concatMap, I don't know what searchValue$ is doing...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.