我想知道如何创建一个新的observable,它将返回两个其他observable中的一个,基于第一个observable是否为空(默认情况下不调用第二个observable)。例如:
// scenario 1
let obs1 = Observable.empty();
let obs2 = Observable.of([1,2,3]);
// This line doesn't work, but is essentially what I'm attempting to do
let obs3 = (obs1.isEmpty()) ? obs2 : obs1;
obs3.subscribe( i => console.log('Next: ', i));
// Next: 1
// Next: 2
// Next: 3
// Complete
// scenario 2
let obs1 = Observable.of([6,7,8]);
let obs2 = Observable.of([1,2,3]);
// This line doesn't work, but is essentially what I'm attempting to do
let obs3 = (obs1.isEmpty()) ? obs2 : obs1;
obs3.subscribe( i => console.log('Next: ', i));
// Next: 6
// Next: 7
// Next: 8
// Complete

答案 0 :(得分:1)
你可以尝试:
let obs3 = obs1.isEmpty().map(x => x? obs2 : obs1;)
现在可以快速检查是否可行。
是的,根据doc,它应该没问题,因为isEmpty
会发出一个布尔值,如果observable为空,则为true,因此你可以使用它来获取你想要的observable。