Wait Observable

Hi, I am developing a login service and I want to wait for an observable get user data. But when I use .toPromise, execution freezes. If I set a timeout, a timeout error will be generated.

This is my code

 let vaCollection = this.firestore.collection<TUsuario>('usuario');
  try {
      let vaUsuarios = await vaCollection.valueChanges().pipe(timeout(1500)).toPromise();
      //never get here
      for (let vaUs of vaUsuarios) {
        console.log(vaUs);
      }
    } catch (error) {
      console.log('Erro na busca: '+ error);
    }

This is my dependencies

"dependencies": {
    "@angular/common": "~8.1.2",
    "@angular/compiler": "~8.1.2",
    "@angular/core": "~8.1.2",
    "@angular/fire": "^5.2.1",
    "@angular/forms": "~8.1.2",
    "@angular/platform-browser": "~8.1.2",
    "@angular/platform-browser-dynamic": "~8.1.2",
    "@angular/router": "~8.1.2",
    "@ionic-native/core": "^5.0.0",
    "@ionic-native/splash-screen": "^5.0.0",
    "@ionic-native/status-bar": "^5.0.0",
    "@ionic/angular": "^4.7.1",
    "br-mask": "0.0.8",
    "cordova-android": "^8.1.0",
    "core-js": "^2.5.4",
    "date-fns": "^2.4.1",
    "firebase": "^6.6.2",
    "rxjs": "~6.5.1",
    "tslib": "^1.9.0",
    "zone.js": "~0.9.1"
  },

What’s the problem?

Try to get the data with observable and subscribe pattern without all other stuff. The way thr docs show. Can u get the data then?

A 1.5 sec timeout maybe also short

Yes, i can get data with observables and subscribe. But I wanted to use Promise so I didn’t have to change the structure of my code. I figured .toPromisse was supposed to do exactly what I wanted, which was to wait for an observable finish.
Regarding the timeout, I already tried without it, and the execution freezes.

The firebase method never finishes so the promise will never resolve

Its a realtime watcher until u tell to complete

For indtance using take(1) in the pipe

1 Like

That was it, using take worked. Thank you.