在 Observable 上使用 await 时,我应该使用什么来代替 toPromise()? [英] What should I use instead of toPromise() when using await on an Observable?
本文介绍了在 Observable 上使用 await 时,我应该使用什么来代替 toPromise()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
此页面说"toPromise 已被弃用!(RxJS5.5+)"
但我最近一直在 AngularFire2 中使用它(当我只想要一个结果时),如下所示:
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();
我不应该这样做吗?如果不是,await
替代方案是什么?
Should I not be doing this? If not, what is the await
alternative?
更新:
在下面的答案之后我改变了这个:
After the answer below I've changed this:
const foo = await this.afs.doc(`docPath`).valueChanges().toPromise();
...至此:
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.
推荐答案
你应该把管道放在后面!
You just should put after pipe!
.pipe(take(1)).toPromise
这篇关于在 Observable 上使用 await 时,我应该使用什么来代替 toPromise()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文