javascript - 如何在 Array.prototype.map 中使用 await 来等待 RxJS Observables
问题描述
如何在 getFooBar 返回 observable 的 map 函数中等待结果?
防爆代码:
this.fooBarResponse.fooBars.map(async (x) => {
x.fooBar = await this.fooBarService
.getFooBar(x.fooBarId.toString())
.toPromise();
return x;
});
foobar.service.ts
getFooBar(id: string): Observable<FooBar> {
return this.fooBarsCollection.doc<FooBar>(id).valueChanges();
}
解决方案
Array#map
是同步的。请改用for...of
循环。
for(const x of this.fooBarResponse.fooBars){
x.fooBar = await this.fooBarService
.getFooBar(x.fooBarId.toString())
.pipe(take(1))
.toPromise();
}
推荐阅读
- couchdb - 更新设计文档时,CouchDB 是否会无条件地重建索引?
- python - 正则表达式:提取 [..] 之间的字符串
- node.js - 当一组用户完成一项任务时,如何执行一项任务?这可以使用 nodejs 和 mongodb 来完成吗?
- python - alembic.util.exc.CommandError:找不到由“f1942fde9843”标识的修订版
- python - 为什么scrapy 说我正在迭代一个“itemMeta”对象?
- r - R中的共现矩阵,对变量的值求和以填充单元格
- regex - ingress rewrite-target 中编号占位符的含义是什么?
- c# - 编写 ASP.NET 自定义存储提供程序
- python - 如何将个人模块导入气流
- angular - Angular 10 - 如何在集合更改时自动加载新项目