【问题标题】:combineLatest does not work with promisescombineLatest 不适用于承诺
【发布时间】:2020-12-06 22:29:57
【问题描述】:

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

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

【问题讨论】:

    标签: angular promise async-await rxjs


    【解决方案1】:

    当您使用.toPromise() 时,流必须已经结束,它才能被转换为promise 并且应用程序才能继续。当 observable 是一个长寿的(就像你拥有的一样)时,它永远不会解决。

    尝试使用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
    

    【讨论】:

      猜你喜欢
      • 2019-11-01
      • 2018-01-06
      • 2017-03-20
      • 2015-07-26
      • 1970-01-01
      • 2022-08-21
      • 1970-01-01
      • 2015-04-18
      • 1970-01-01
      相关资源
      最近更新 更多