2016-11-17 8 views
1

私は観測値から値を受け取り、観測値を返す2つの関数に値をパイプする方法が必要です(単一の値を出してから終了する)。私は.combineLatest()が私に投影関数を渡すことを望んでいましたが、そうではありません。RxJS5:投影関数を持つ.combineLatest()?

サンプルコード(動作しない):

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 
    }) 
); 

それを行う方法上の任意のアイデア?

答えて

1

combineLatest演算子を使用する考えは正しいですが、間違った場所にあります。 observablesを平坦化する必要がある場合は、mergeMapを使用する必要があります。これは、あなたが何を期待しているJSBINです:http://jsbin.com/rutovot/4/edit?html,js,console

コードは次のようになります。

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)); 
+0

あなたがそれを読んでたらそう些細な:) – Dyna

+0

は、多くの場合、RxJSであるが、それは、それに使用を得るために、いくつかの時間がかかります:) – KwintenP

関連する問題