将外部和内部 observable 的结果组合到下一个 map 方法的单个参数(数组)中

Combining the results of an outer & inner observable into a single parameter (array) for the next map method

我正在尝试做的类似于 combineLatest,但第二个(内部)可观察到的取决于第一个的响应

ajax.get('/first-url')
  .map(res => new Thing(res.response)
  .someMap(thing => 
    ajax.get(`/second-url?code=${thing.code}`)
      .map(res => new OtherThing(res.response))
  ).map(([firstThing, secondThing]) => console.log(firstThing, secondThing))

基本上,我希望能够将这两个结果合并到一个数组中,我可以用它来做第三件事。

我的目标是获取数据A和数据B,因为我需要它们同时执行某个操作,而获取数据B依赖于已有的数据A。

我可以通过执行以下操作来实现此功能:

ajax.get('/first-url')
  .map(res => new Thing(res.response)
  .mergeMap(firstThing=> 
    ajax.get(`/second-url?code=${firstThing.code}`)
      .map(res => new OtherThing(res.response))
      .map(secondThing => [firstThing, secondThing])
  ).map(([firstThing, secondThing]) => console.log(firstThing, secondThing))

本质上,在第二个可观察对象的响应中,return 我想在外部可观察对象的 .map

中使用的确切数组

这似乎是 rxjs 想要的处理方式。在 5.x 的 rxjs 版本中,有一个叫做 Result Selector 的东西,它可以作为第二个参数传入来创建这样的东西(它看起来很像最后一个内部 .map)但是对于 rxjs v6.0,这似乎已被弃用:

https://github.com/ReactiveX/rxjs/blob/master/docs_app/content/guide/v6/migration.md#howto-result-selector-migration