combineLatest does not work with promises

Viewed 994

I want to execute some sequential code but I cannot extract the data from 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 Answers

When you use .toPromise(), the stream has to have ended for it to be converted to a promise and for the application to proceed. When the observable is a long living one (like the one you have), it won't ever resolve.

Try using the take operator to finish the 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
Related