在 Observable 上使用 await 时,我应该使用什么来代替 toPromise()?

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

This page"toPromise has been deprecated! (RxJS 5.5+)" 但我最近一直在将它与 AngularFire2 一起使用(当我只想要一个结果时),如下所示:

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

我不应该这样做吗?如果不是,await 替代方案是什么?

更新:

在下面的答案之后我改变了这个:

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))));

有人可以向我解释一下这是如何改进的吗?!对我来说似乎是倒退了一步。

你应该放在管道后面!

   .pipe(take(1)).toPromise

对于那些想要疯狂的人来说,还有一些其他的选择:

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

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

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

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

但最短的是:

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

在任何可以使用 take(1) 的地方,如果你想发出错误,你可以使用 first()

有关更多 Firebase 承诺,请参阅 here

J