Mateuszto
Mateuszto

Reputation: 1

How can I reduce subscriptions in RxJs and simplify my code

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

Upvotes: 0

Views: 76

Answers (1)

Zerotwelve
Zerotwelve

Reputation: 2361

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);

Upvotes: 1

Related Questions