最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - What should I use instead of toPromise() when using await on an Observable? - Stack Overflow

programmeradmin1浏览0评论

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(`docPath`).valueChanges().toPromise();

Should I not be doing this? If not, what is the await alternative?

UPDATE:

After the answer below I've changed this:

const foo = await this.afs.doc(`docPath`).valueChanges().toPromise();

...to this:

const foo = await (new Promise(resolve => this.afs.doc(`docPath`).valueChanges().pipe(first()).subscribe(result => resolve(result))));

Could someone please explain to me how this is an improvement?! Seems like a step backward to me.

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(`docPath`).valueChanges().toPromise();

Should I not be doing this? If not, what is the await alternative?

UPDATE:

After the answer below I've changed this:

const foo = await this.afs.doc(`docPath`).valueChanges().toPromise();

...to this:

const foo = await (new Promise(resolve => this.afs.doc(`docPath`).valueChanges().pipe(first()).subscribe(result => resolve(result))));

Could someone please explain to me how this is an improvement?! Seems like a step backward to me.

Share Improve this question edited Aug 3, 2018 at 20:34 Jus10 asked Aug 3, 2018 at 17:20 Jus10Jus10 15.7k22 gold badges58 silver badges80 bronze badges 4
  • 1 Possible duplicate of Angular/rxjs: Why don't I have to import toPromise anymore? – zero298 Commented Aug 3, 2018 at 17:24
  • 4 @zero298 don't dupe vote just because toPromise appears in both questions... – Jonas Wilms Commented Aug 3, 2018 at 17:25
  • @JonasW. Did you read the duplicate? "toPromise: now exists as a permanent method on Observable" I take that to mean it isn't deprecated. Would you rather me post an answer that says "It's not deprecated" or link to a question that says as much? – zero298 Commented Aug 3, 2018 at 17:28
  • @zero298 the question is 1 year old? – Jonas Wilms Commented Aug 3, 2018 at 17:30
Add a comment  | 

3 Answers 3

Reset to default 16

You just should put after pipe!

   .pipe(take(1)).toPromise

firstValueFrom and lastValueFrom is definitly a better alternative for many reasons:

  1. The naming is more readable and self explanatory.
  2. The additional ability to select either first or last value.
  3. The additional ability to declare a default value in case the observable didn't emit any

Reference: https://stackoverflow.com/a/69215890/5191209

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

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

or

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

or

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

or

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

But the shortest is:

const foo = (await this.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

发布评论

评论列表(0)

  1. 暂无评论