Skip to content Skip to sidebar Skip to footer

What Should I Use Instead Of Topromise() When Using Await On An Observable?

This page says 'toPromise has been deprecated! (RxJS 5.5+)' but I've been using it lately with AngularFire2 (when I only want one result) like this: const foo = await this.afs.doc(

Solution 1:

You just should put after pipe!

.pipe(take(1)).toPromise

Solution 2:

Just a few other options for those who want to be crazy:

const foo = awaitthis.afs.doc(`docPath`).valueChanges().pipe(take(1)).toPromise();

or

const foo = (awaitthis.afs.doc('docPath').get().toPromise()).data();

or

const foo = (awaitthis.afs.doc('docPath').get().pipe(take(1)).toPromise()).data();

or

const foo =  (awaitthis.afs.doc('docPath').snapshotChanges().pipe(take(1))
.toPromise()).payload.data();

But the shortest is:

const foo = (awaitthis.afs.doc('docPath').ref.get()).data();

And anywhere you can use take(1) you can use first() if you want to emit an error.

For more Firebase promises, see here.

J

Post a Comment for "What Should I Use Instead Of Topromise() When Using Await On An Observable?"