在 RxJava 中有没有像 Promise.all() 这样的方法? [英] Is there a way like Promise.all() in RxJava?

查看:74
本文介绍了在 RxJava 中有没有像 Promise.all() 这样的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题.

我需要通过项目 ID 列表来获取一些项目的列表.起初,我尝试了

I need to get list of some items by a list of the item ids. At first, I tried

Observable.from(itemIds)
  .flatMap(itemId -> requestToServer(itemId))
  .subscribe(item -> { /* do something */ });

但是操作符flatMap 不保证项目的顺序.我需要按 ItemIds 的顺序获取项目.

But the operator flatMap does not guarantee the order of items. I need to get item in order that ItemIds have.

如果有像 Promise.all() 这样的 API 就好了.在 RxJava 中有没有类似 Promise.all() 的方法?或任何其他方式?

It would be great if there was the api like Promise.all(). Is there a way like Promise.all() in RxJava? or any other ways?

推荐答案

使用 concatMap 代替.这将按顺序连接发射的 Observables,而不是合并它们的发射

Use concatMap instead. That will concatenate the emitted Observables in order rather than merging their emissions

返回一个新的 Observable,它发出由应用您提供给源发出的每个项目的函数Observable,该函数返回一个 Observable,然后发出由连接结果产生的项目可观察的.

Returns a new Observable that emits items resulting from applying a function that you supply to each item emitted by the source Observable, where that function returns an Observable, and then emitting the items that result from concatinating those resulting Observables.

这篇关于在 RxJava 中有没有像 Promise.all() 这样的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆