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

254
Vistas
How to connect two observable and return true if they completes in Angular using RxJs?

I'm creating a guard and I'm using canActivate. I'm also using NgRx state management.

Scenario:

If materialState$ has data, return true
If userCurrency$ has data, return true

I would like to connect them into one observable and if their both have data return true

My code:

export class IsTrue implements CanActivate {
   constructor(private userFacade: UserFacade) {}
    
   canActivate(): Observable<boolean | UrlTree> | boolean | UrlTree {
      this.userFacade.getUserStatiscis();
    
      const materialState$ = this.userFacade.materialState$.pipe(
          filter((data) => data !== null),
          map((data) => {
             if (data) return true;
          }),
      );
    
      const userCurrency$ = this.userFacade.selectUserCurrency$.pipe(
          filter((data) => data !== null),
          map((data) => {
             if (data) return true;
          }),
      );
    
// return forkJoin(materialState$, userCurrency$);
    }
}

I've tried using forkJoin, merge, and concat but without success.

@Edit

I found a working "solution", could you rate that?

      return combineLatest([materialState$, userCurrency$]).pipe(
            map((data) => {
                if (data) return true;
            }),
        );
over 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

You can achieve that using combineLatest operator, then mapping the result to a boolean, like the following:

combineLatest([
  this.userFacade.materialState$.pipe(filter(data => !!data)),
  this.userFacade.selectUserCurrency$.pipe(filter(data => !!data))
]).pipe(
  map(([materialState, userCurrency]) => !!materialState && !!userCurrency)
);
over 4 years ago · Santiago Trujillo Denunciar

0

If all you want to know is whether they both emitted at least one value combineLatest is your friend, because it only starts to emit after all sources emitted at least once.

combineLatest([
  this.userFacade.materialState$,
  this.userFacade.selectUserCurrency$
]);

if you explicitely need the true/false you can append

.pipe(
  map(() => true)
);
over 4 years ago · Santiago Trujillo Denunciar

0

const materialState$ = this.userFacade.materialState$.pipe(
    filter(data => !!data)
);

const userCurrency$ = this.userFacade.selectUserCurrency$.pipe(
    filter((data) => !!data)
);


const connectedStream$ =  combineLatest([ materialState$, userCurrency$ ]);
over 4 years ago · Santiago Trujillo 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