2017-02-22 6 views
0

現在、IFごとに関数isEvenとisOddが呼び出されています。 IFの評価が関数の論理パスに対応する場合にのみ関数が呼び出される方法はありますか?関数を呼び出すことなくObservable.Ifを使用する方法

例:JSBinの参照:http://jsbin.com/wegesaweti/1/edit?html,js,output

var isEven = function(x) { 
    console.log('function isEven called') 
    return Rx.Observable.return(x + ' is even'); 
};  
var isOdd = function(x) { 
    console.log('function isOdd called') 
    return Rx.Observable.return(x + ' is odd'); 
}; 
var source = Rx.Observable.range(1,4) 
    .flatMap((x) => Rx.Observable.if(
    function() { return x % 2; }, 
    isOdd(x), 
    isEven(x) 
));  
var subscription = source.subscribe(
    function (x) { 
     console.log('Next: ' + x); 
    }); 

電流出力:

function isOdd called 
function isEven called 
Next: 1 is odd 
function isOdd called 
function isEven called 
Next: 2 is even 
function isOdd called 
function isEven called 
Next: 3 is odd 
function isOdd called 
function isEven called 
Next: 4 is even 

の予想される出力

function isOdd called 
Next: 1 is odd 
function isEven called 
Next: 2 is even 
function isOdd called 
Next: 3 is odd 
function isEven called 
Next: 4 is even 

ありがとうございました! RXJS docuemntation

答えて

1

ベース(elseSourceまたはSchedulerObservableすべきthenSourceelseSource必要。あなたの問題のため

代替:

var source = Rx.Observable.range(1,4).flatMap((x) => 
    (x % 2) == 1 ? isOdd(x) : isEven(x) 
); 

の作業例:http://jsbin.com/godubemano/1/edit?html,js,output

+0

ありがとうございます!うまくいきます! –

関連する問題