Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

121
Views
Returning a combineLatest observable from a function with an argument

I'm not really sure if I'm doing this correctly.

I have a simple function that has an argument which I want to use inside a combineLatest observable. That observable is then returned.

  fn(arg) {
    return combineLatest([
      observable...,
      observable...,
    ]).pipe(
      map(() => arg)
    );
  }

The issue here is that when the function is called many times, it creates duplicate observables.

What would be the proper solution? To make the argument an observable too? So the function is only called once but it returns the correct value when the argument changes.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Solution is not use Observables this way. Use a Subject for that kind of things:

readonly actionSubject = new Subject<unknown>();

...

ngOnInit(): void {
  this.actionSubject.pipe(
    switchMap(newValue => 
      combineLatest([
        observable...,
        observable...,
      ]).pipe(map(() => newValue)),
    )
  ).subscribe();
}


...
ngOnDestroy(): void {
  this.actionSubject.complete();
}

And in place of your function usage instead of fn:

this.actionSubject.next(someNewValue);

What happens here? SwitchMap will stop you old thread and start new one every time you have new value. That's solution.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!