Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

125
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda