12
votes
var arr = [obs1, obs2, obs3];
Observable.forkJoin(...arr).subscribe(function (observableItems) {})

Runs observables in parallel and return an array.

How can I run the observables sequentially and return an array. I do not want to get called for each observable, so concat() is not appropriate for me.

I want to receive the same final result as forkJoin but run sequentially.

Does it exist? or do I have to code my own observable pattern?

1
It seems that you're looking for merge ... (even tho it does not return an array)maxime1992
Why concat is not an option? - You can call toArray on concatenation result.Bogdan Savluk
Concat is not an option because he wants them to run in //, concat will not run them in //maxime1992

1 Answers

19
votes

Just use concat and then toArray:

var arr = [obs1, obs2, obs3];
Observable.concat(...arr).toArray().subscribe(function (observableItems) {})

If you need behavior, more similar to forkJoin(to get only last results from each observables), as @cartant mentioned in comment: you will need to apply last operator on observables before concatenating them:

Observable
  .concat(...arr.map(o => o.last()))
  .toArray()
  .subscribe(function (observableItems) {})