CombineLatest不能与诺言一起使用

时间:2020-08-17 18:35:17

标签: angular promise async-await rxjs

我想执行一些顺序的代码,但是我无法从CombineLatest中提取数据

const { datosPersona, participante } = await combineLatest(
  this.store.select('datosPersona'),
  this.store.select('participante')
).pipe(
  map((data) => ({ datosPersona: data[0], participante: data[1] }),
).toPromise();

1 个答案:

答案 0 :(得分:2)

使用.toPromise()时,流必须结束才能将其转换为Promise,然后应用程序才能继续。如果可观察到的是寿命长的一个(就像您拥有的那样),它将永远无法解决。

尝试使用take运算符完成流。

import { take } from 'rxjs/operators';
.....
const { datosPersona, participante } = await combineLatest(
  this.store.select('datosPersona'),
  this.store.select('participante')
).pipe(
  map((data) => ({ datosPersona: data[0], participante: data[1] }),
  take(1), // take the first emission and only the first one
).toPromise(); // now it can be converted to a promise since the stream finished