Rxjs Observable run sequentially and return an array

不问归期 提交于 2020-01-23 05:47:08

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!