Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

126
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda