LppEdd
LppEdd

Reputation: 21104

RxJs - waiting for all Observables to finish

I've got this piece of code:

myModel.tags
       .filter(...)
       .map(...)
       .forEach(t => context.dispatch(new MyAction(t)))

dispatch returns an Observable.
I'd like to execute other RxJs code only when those Observables are all completed.
Is that possible?

Upvotes: 2

Views: 1078

Answers (1)

martin
martin

Reputation: 96891

If you want to know when all Observables complete you have to collect all of them into an array and then use ideally forkJoin:

const obs = myModel.tags
  .filter(...)
  .map(...)
  .map(t => context.dispatch(new MyAction(t)))

const done$ = forkJoin(...obs).subscribe(...);

The forkJoin observable creation method will emit just once with the array of all responses and then complete.

Upvotes: 2

Related Questions