7

Let's say I dynamically create an array of observables:

let obsArray = dataArray.map((data) => observableFunc(data))

I want to execute each of these observables in order, executing obsArray[n+1] only once obsArray[n] has completed. The execution of the next observable DOES NOT depend upon the result of the previous observable.

After the each observable in the array has completed, I want to return from the function that is handling this array of observables.

Essentially, I would like to find a method like forkJoin that ensures sequential execution.

Connor O'Doherty
  • 385
  • 1
  • 5
  • 19
  • I read it as the signature of concat() does not accept arrays. Looking at it again, maybe I was wrong? EDIT: nevermind, it will not accept an array of observables – Connor O'Doherty Oct 26 '17 at 20:59
  • This question isn't the same as the one marked as having an answer already. This question is about array of observables not standalone observables. I'm having the same issue with firing an array of observables in a sequence. – Chrillewoodz Sep 03 '18 at 10:19

1 Answers1

10

As already mentioned, concat operator can align emitted values in a row. You can pass array of observable to it as follows (thx to ES6 spread operator!):

const obsArray = [
  Rx.Observable.of(1, 2, 3), 
  Rx.Observable.of(4, 5, 6), 
  Rx.Observable.of(7, 8, 9)];

const example = Rx.Observable.concat(...obsArray);
// or using old apply method
// Rx.Observable.concat.apply(this, obsArray); 

// prints: 1,2,3,4,5,6,7,8,9
const subscribe = example.subscribe(val => console.log(val));
udalmik
  • 7,838
  • 26
  • 40