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

293
Vistas
Making an Observable from a callback

I have an auth guard that needs an asynchronous response true/false when the site is visited and the user is already logged in.

I'm using Firebase's onAuthStateChanged (link to docs) and it uses a callback function. How can I turn my isLoggedIn() method into something that can return Observable<boolean>?

Typscript:

get isLoggedIn(): Observable<boolean> {

    // want something like this:
    return Observable.fromCallback(firebase.auth().onAuthStateChanged).map(user => !!user);

    // this returns () => boolean, but I need a promise or observable
    return firebase
      .auth()
      .onAuthStateChanged((user) => {
        return !!user;
      });

}
about 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

You can do it like this.

get isLoggedIn(): Observable<boolean> {

  // want something like this:
  return Observable.create(
    observer => firebase
        .auth()
        .onAuthStateChanged((user) => {
          observer.next(!!user)
        });
    );
}
about 4 years ago · Santiago Trujillo Denunciar

0

there is one more way of doing this using bindCallback from rxjs/observable/bindCallback

const observable=bindCallback(functionWhichExpectsCallBack)

e.g

function foo(value, callback) {
  callback(value);
}
//converts callback to observable
    const observable = bindCallback(foo); 
    observable(1)
      .subscribe(console.log); 
about 4 years ago · Santiago Trujillo Denunciar

0

Note: bindCallback and Observable.create were deprecated,
you could use new Observable instead:

...
return new Observable((subscriber) => {
  firebase
    .auth()
    .onAuthStateChanged((user) => {
      subscriber.next(user);
    })
}
about 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