Consistently execute observables and emit one result

I have an array of observables that I execute in parallel using:

let observables: Observable<any>[]

Observable.forkJoin(observables) 

This works fine, however I need to sequentially execute an array of observables and only emit one result if the last observable has been completed. This is when I tried to use

Observable.concat(observables)

But this returns several results, and not just one - the combined result that I get when using forkJoin. So I really need a combination of the two.

I tried using the reduction function to execute them sequentially, for example:

return observables.reduce((previous, current) => {
  return previous.flatMap(() => current);
}, Observable.empty());

But with this decision, observables are not fulfilled at all.

+4
source share
2 answers

, obserables , , - :

return Observable.concat(...observables).reduce((acc, current) => [...acc,current], []);

:

return Observale.concat(...observables).toArray();

, , :

const source= Observable.concat(...observables).flatMap(list => list).toArray();
+2

toArray():

Observable.concat(observables).toArray().subscribe()

RxJS: " , , ".

+3

Source: https://habr.com/ru/post/1692458/


All Articles