首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >RxJS5:具有投影函数的.combineLatest()?

RxJS5:具有投影函数的.combineLatest()?
EN

Stack Overflow用户
提问于 2016-11-17 10:01:52
回答 1查看 983关注 0票数 1

我需要一种方法来将值从可观察的传递到两个函数,每个函数接受一个值,然后每个函数返回一个可观察的值(只发送一个值,然后完成)。我希望.combineLatest()允许我传递一个投影函数,但它没有。

示例代码(不起作用):

代码语言:javascript
复制
const ac = [1, 2, 3]; // only as example, I have complex types in my array not numbers

Observable.from(ac)
    .combineLatest(
        // processFn should get a number as argument and return Observable<number>
        // does not work because .combineLatest does not accept two functions as arguments :(
        n => processFn1(n),
        n => processFn2(n)
    )
    .map(([result1, result2] => {
        // result1, result2 should be flat numbers here, not Observables
    })
);

知道怎么做吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-11-17 12:44:06

您使用combineLatest操作符的想法是正确的,但它在错误的位置。如果您需要使用可观察到的数据,则应该使用mergeMap。这是一个符合您预期的JSBIN:http://jsbin.com/rutovot/4/edit?html,js,console

代码如下所示:

代码语言:javascript
复制
const ac = [1, 2, 3];

Rx.Observable.from(ac)
  // use mergeMap, it takes a function that accepts a value and
  // returns an observable. MergeMap will listen to this observable
  // under the hood and next the result of this observable down the
  // the chain
  .mergeMap(val => {
    // Here we return an observable that combines the result of the
    // call to both the functions
    return Rx.Observable.combineLatest(fake(val), fake2(val));
  })
  .subscribe(val => console.log(val));
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40651842

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档