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?
In parallel computing, the fork–join model is a way of setting up and executing parallel programs, such that execution branches off in parallel at designated points in the program, to "join" (merge) at a subsequent point and resume sequential execution.
It's worth noting that the forkJoin operator will preserve the order of inner observables regardless of when they complete.
There is no next() on Observable; only on Subject and BehaviorSubject, which extends Subject (and both extend Observable).
If no input observables are provided (e.g. an empty array is passed), then the resulting stream will complete immediately. forkJoin will wait for all passed observables to emit and complete and then it will emit an array or an object with last values from corresponding observables.
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) {})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With