简体   繁体   English

combineLatest 不适用于承诺

[英]combineLatest does not work with promises

I want to execute some sequential code but I cannot extract the data from combineLatest我想执行一些顺序代码,但我无法从 combineLatest 中提取数据

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

When you use .toPromise() , the stream has to have ended for it to be converted to a promise and for the application to proceed.当您使用.toPromise()时,stream 必须已结束才能将其转换为 promise 并继续应用程序。 When the observable is a long living one (like the one you have), it won't ever resolve.当 observable 是一个长寿的(就像你拥有的那样)时,它永远不会解决。

Try using the take operator to finish the stream.尝试使用take运算符完成 stream。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM