问题
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:
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) {})
来源:https://stackoverflow.com/questions/42865820/rxjs-observable-run-sequentially-and-return-an-array