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

129
Views
Dont return value with observable, from the service layer to the component

I'm trying to pass the return of a service layer with subject to the components.

My service that connects with api:

 public getPerfilNew$(): Observable<PerfilInvestidor> {
            return this.http.get<PerfilInvestidor>(`${environment.api.basePosicaoConsolidada}/consolidado`)
              .pipe(
                map(res => res),
                shareReplay(1),
                catchError(err => {
                    console.log('Error in perfil', err);
                    return throwError(err);
                })
            )
        }

public getPositionConsolidated(): Observable<PosicaoConsolidada> {
    return this.http.get<PosicaoConsolidada>(`${environment.api.basePosicaoConsolidada}/consolidado`)
      .pipe(
        map(res => res),
        shareReplay(1),
        catchError(err => {
            console.log('Error investiments', err);
            return throwError(err);
        })
    )
}

In my component: I tried it

public loadData() {
        //find out if the profile failed or no
        this.homeGuardService.getPerfilNew$.pipe(
            takeUntil(this.unsubscribe) 
        ).subscribe(res => {
            this.perfilInvestidor = res;
            this.perfilFailed = this.perfilInvestidor.status ? true : false;
            console.log('perfil is failed --->', this.perfilFailed)
        })

        //checking for investments
        this.homeGuardService.getPositionConsolidated().subscribe(res => {
            this.positionConsolidated = res
            if (this.positionConsolidated) {
                this.investments = true
            }
        });


        this.isEverthingFailed = !this.investments && this.perfilFailed
}

I need external subscribe values ​​to match them in my variable isEverthingFailed teria que utilizar subject? BehaviorSubject? Because that way the variable investments and perfilFailed is undefined

With that amount of observables I compromise the memory? I'm open to suggestions

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

0

You can use forkJoin or combineLatest to get both the streams and subscribe to them:

public loadData() {
  forkJoin([this.homeGuardService.getPerfilNew$(), this.homeGuardService.getPositionConsolidated()])
    .subscribe(([perfile, position]) => {
        this.perfilInvestidor = perfile;
        this.perfilFailed = this.perfilInvestidor.status ? true : false;
        console.log('perfil is failed --->', this.perfilFailed)
        this.positionConsolidated = position
        if (this.positionConsolidated) {
            this.investments = true
        }
        this.isEverthingFailed = !this.investments && this.perfilFailed
    })
  )
}
about 4 years ago · Juan Pablo Isaza Report

0

1. Questions

There is no enough code here to see exactly what is happening. But at first glance it looks like a race condition from my point of view:

You have to realise how Subject works.

const sub = new Subject();
sub.next(1);

// If you subscribe here like:
   sub.subscribe(a => console.log(a))
// Nothing will happen because Subject does not preserve the old values

But if you do this:

const sub = new Subject();

// If you subscribe here like:
sub.subscribe(a => console.log(a))

sub.next(1);

// You will get a print in a console since you emitted the value after you subscribed on the stream.

In your case it looks like that you are subscribing to a subject after subject already emitted values.

Also:

 this.perfilClient$.subscribe(res => {

You are subscribing to this.perfilClient$, but i dont see where you are emitting values on that stream?

So if you cannot sync things that you subscribe to the Subject before it emits the value, you can use ReplaySubject or BehaviourSubject

2. Questions You have just a few observables, its not much. But there are are some antipatterns such as:

  1. Nesting subscribing
  2. Subscribe callback function is empty etc..

The main thing in RXJs is to avoid side effects as much as possible.

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!